![]() |
Native Integer Incrementing (Existing Value)
Hi guys, I have a rather simple issue. I have a SMALLINT column, and I want this value to rise by one each-time I use that row (Basically this is recording the number of views on that row).
At the moment I am doing this (Stripped-down for the example): PHP Code:
1. Is there a way to increment an existing column without specifically calling UPDATE on it? .. else.. 2. Is there a way to increment it with SQL, so I can just do this in the query? |
There's nothing like that built-in to my knowledge. Your method is fine.
|
Quote:
Pedant or Guelphdad may be able to verify; a custom procedure should let you get away with doing that. But then you need to execute via call, so I'm not sure if that would be more ideal or not. |
OF COURSE you should use a STORED PROCEDURE!
Trivial. Example: Code:
mysql> select * from foo;Why do you thing executing via CALL would be a bad thing, FouLu? Most DB experts would tell you that you should do *ALL* work via Stored Procedures and not give the casual DB client permissions to use anything *except* Stored Procs! |
Incidentally, the Stored Procedure will be roughly twice as efficient as making the two separate calls ( SELECT and then UPDATE ) as proposed in the first post.
That's because your PHP code only needs to make one interprocess invocation of MySQL. People don't realize how very expensive interprocess calls are. The actual time spent in the DB is going to be roughly the same. The first query (whether it be the SELECT as first proposed or the UPDATE in my procedure) is going to ensure that the relevant record is fetched from disk to memory and so will be cached in memory for the second operation in both cases. So the disk-to-memory overhead will be the same and you are thus saving one interprocess call. Nothing to be sneezed at. |
Heey, that looks nice. I want to use stored procedures for everything it looks simple!
Quote:
Thanks for the tips too. I usually assume everything to do with the database is resource expensive, so things like doing one query instead of two if I can is usually in the back of my mind. That last comment about caching, do you mean that — in your example, for example — the select query will use the incremented viewcount from memory of the first line? |
The SELECT would use the incremented value even if caching wasn't involved. SQL guarantees that operations will be done in the order requested.
The remark about caching has to do with the fact that, like most modern DB engines, MySQL makes use of a *HUGE* in-memory cache. Many many megabytes. (We actually had one system where we "tuned" MySQL to use 75% of available memory. Yes, you can do that. By default, though, it uses a lot less than that.) So when you make a query of any kind on a given record, the disk *BLOCK* that contains that record is loaded into memory. And the DB engine kind of assumes that the most recent record fetches are going to be needed again, so it KEEPS that block in memory as long as it can (that is, it caches it). In a typical MySQL installation, you can pretty much count on any record that was fetched till being in the cache many seconds or even a minute or two after it was fetched. And clearly if you do an UPDATE immediately followed by a SELECT as I do in the Procedure--or even if you do two separate queries, SELECT followed by UPDATE as in your first post--the record *WILL* still be in memory. So the second operation doesn't involve use of the disk at all. MySQL just remembers where it put that block in its memory cache and ZAM! It's all done in memory. (Of course, after an UPDATE, eventually that disk block needs to be written back out to the disk, but MySQL does a lazy update, trying to schedule the writes when no other user-requested operations are taking place at a higher priority.) |
And of course you can increment a field as shown there.
You can do a lot more than that: Code:
UPDATE sometable SET field1 = field1 + 1 + IF(field2 > 3, 7, -12 )Heck, you can even "tack on" to text fields: Code:
UPDATE sometable SET ticks = CONCAT( ticks,'*') WHERE id = 1817 |
Great, this should all make my scripts nice and neater! But, I see maybe a problem..
Can I use PHP variables in stored procedures? I couldn't get them to work in views.. |
You can't use PHP variables *DIRECTLY*, but you can pass them in as parameters.
For example, using the getAndBumpCount SP that I created, you would invoke it from PHP via: Code:
$sql = "call getAndBumpCount ('$name')";In my SP, I used Code:
create procedure getAndBumpCount( nm varchar(20) )Some people use a naming convention for parameters. Such as P_NM, P_ID, P_Address, etc. Where the "P_" makes it clear that this variable is a "P"arameter to the stored procedure. |
Brilliant, OK, now to put it all to good use. Thanks for the expert advice Old Pedant, I feel a little more confident in my database operations now.
|
| All times are GMT +1. The time now is 03:47 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.