Re: [SQL] concurrency problem
On Sat, Jun 17, 2006 at 09:23:17AM +0530, sathish kumar shanmugavelu wrote: > I fetch the consultatioin_no and add one to it, i should know this > consultation_no to save the other 10 tables. because i use this number as Don't do that. Fetch the number from a sequence first: select nextval(). Then you can insert it as currval() for all your other INSERTs. No, there is no race condition or concurrency problem: see the docs on these functions. No locks. A -- Andrew Sullivan | [EMAIL PROTECTED] When my information changes, I alter my conclusions. What do you do sir? --attr. John Maynard Keynes ---(end of broadcast)--- TIP 1: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to [EMAIL PROTECTED] so that your message can get through to the mailing list cleanly
Re: [SQL] keeping last 30 entries of a log table
On Sat, 17 Jun 2006, Daniel CAUNE wrote: insert into log (account_id, message) values (1, 'this is a test); delete from log where account_id = 1 and id not in ( select id from log where account_id = 1 order by timestamp desc limit 30); I'm wondering if there is a more performance oriented method of doing the delete that I'm not thinking of. Depending on whether id is a kind of auto-incremented column that never cycles, I would suggest something like: DELETE FROM log WHERE account_id = 1 AND id < ( SELECT MIN(id) FROM log WHERE account_id = 1 ORDER BY timestamp DESC LIMIT 30); I think there will be a performance difference with your method when the number of records to be deleted is huge. Thanks Daniel, I'll try and benchmark them both and see if < turns out to be faster than NOT IN. I guess there's no way to get around the subselect though. -- Jeff Frost, Owner <[EMAIL PROTECTED]> Frost Consulting, LLC http://www.frostconsultingllc.com/ Phone: 650-780-7908 FAX: 650-649-1954 ---(end of broadcast)--- TIP 6: explain analyze is your friend
Re: [SQL] keeping last 30 entries of a log table
> >> insert into log (account_id, message) values (1, 'this is a test); > >> delete from log where account_id = 1 and id not in ( select id from log > >>where account_id = 1 order by timestamp desc limit 30); > >> > >> I'm wondering if there is a more performance oriented method of doing > the > >> delete that I'm not thinking of. > >> > > > > Depending on whether id is a kind of auto-incremented column that never > cycles, I would suggest something like: > > > > DELETE FROM log > > WHERE account_id = 1 > >AND id < ( > > SELECT MIN(id) > >FROM log > >WHERE account_id = 1 > >ORDER BY timestamp DESC > >LIMIT 30); > > > > I think there will be a performance difference with your method when the > number of records to be deleted is huge. > > Thanks Daniel, I'll try and benchmark them both and see if < turns out to > be > faster than NOT IN. I guess there's no way to get around the subselect > though. > Column id should be indexed indeed. Anyway, I'm not sure about any performance improvement using that last method, as the most consuming operation might be the DELETE operation, not really the SELECT operation, when dealing with a huge volume of data. -- Daniel ---(end of broadcast)--- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match