School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,575 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,187 people online right now. Registration is fast and FREE... Join Now!




How to optimize performance using a BEGIN statement

 

How to optimize performance using a BEGIN statement

Mike007

11 Jun, 2009 - 09:15 PM
Post #1

D.I.C Regular
Group Icon

Joined: 30 Aug, 2007
Posts: 306



Thanked: 5 times
Dream Kudos: 75
My Contributions
Hello, I want to replace a large number of individual insert queries with a single statement to get it done faster. I was told the way to do it is by using a BEGIN... END statement in SQLite. But so far i do not understand how it works, code like this doesn't seem to effect the database:

CODE

BEGIN;
    INSERT INTO songs VALUES ('song name', 'artist', 413);
END;


I have tried going through their documentation but it is too general. Just a few diagrams, very unclear.

Right now the application I am developing takes 5 minutes to write ~2000 records using individual inserts, which is unacceptable. What am I doing wrong?

User is offlineProfile CardPM
+Quote Post


mikeblas

RE: How To Optimize Performance Using A BEGIN Statement

12 Jun, 2009 - 02:47 PM
Post #2

D.I.C Regular
Group Icon

Joined: 8 Feb, 2008
Posts: 390



Thanked: 27 times
My Contributions
I don't use SQLLite much, but I expect the advice you were given was to put the bunch of INSERT statements you want into a transaction using BEGIN TRANSACTION And END TRANSACTION.

With a single insert statement in a block, you're not going to notice any difference at all because nothing has changed. What I figure is happening to you is that each insert you make is in its own transaction. Each transaction must be locked, isolated, committed, journaled, and logged atomically, distinct from everything else that is happening to the database at the same time.

If you have 2000 batches of single insert statements, you're doing all the log management steps 2000 times. If you have one batch of 2000 insert statements, you're doing a larger block of log management, but you only do it once. That would cause a performance change, hopefully for the better.

If you're going to do this regularly, you'll want to figure out how to get bulk insert or batch loading working, if SQLite supports it. Such a technique usually locks the table, batches the insert, doesn't log it, and then releases the lock. If that's appropriate for your application, it is generally much (much!) faster than individual inserts in individual transactions, or even a single transaction of many inserts.
User is offlineProfile CardPM
+Quote Post

Mike007

RE: How To Optimize Performance Using A BEGIN Statement

12 Jun, 2009 - 04:10 PM
Post #3

D.I.C Regular
Group Icon

Joined: 30 Aug, 2007
Posts: 306



Thanked: 5 times
Dream Kudos: 75
My Contributions
You are right on the money. That is exactly the advice I got. The only reason I used only one insert there is because I wanted to test it. I have a code that is generating the rest of the inserts. Didn't really want to change the code before I tested it. It did work, I used a simple SQLite database browser to try it out. When I say worked I mean it did not return a syntax error. But the record was not added. Or it was rolled back, but I am not sure. How do I prevent that from happening or make it actually add records?
User is offlineProfile CardPM
+Quote Post

mikeblas

RE: How To Optimize Performance Using A BEGIN Statement

12 Jun, 2009 - 07:22 PM
Post #4

D.I.C Regular
Group Icon

Joined: 8 Feb, 2008
Posts: 390



Thanked: 27 times
My Contributions
You might check the SQLite docs to learn more about transactions and how that database handles them.
User is offlineProfile CardPM
+Quote Post

Mike007

RE: How To Optimize Performance Using A BEGIN Statement

12 Jun, 2009 - 07:23 PM
Post #5

D.I.C Regular
Group Icon

Joined: 30 Aug, 2007
Posts: 306



Thanked: 5 times
Dream Kudos: 75
My Contributions
I did just that, I do not understand how it works. It is too generic and there is no example there :S.
User is offlineProfile CardPM
+Quote Post

Trogdor

RE: How To Optimize Performance Using A BEGIN Statement

14 Jun, 2009 - 04:25 AM
Post #6

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 619



Thanked: 14 times
Dream Kudos: 200
My Contributions
Usually if you start a transaction you need to do a COMMIT before the end, otherwise it is implicitly rolled back.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 08:16AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month