coolguy010 — 2013-07-16T10:01:28-04:00 — #1
i want to know what happens if there is a Insertion query in two function running in parallel or if query is inside a foreach loop..
> one more thing is my table has a AUTO INC field, so if two insertion query runs on same table at the same time , then what happens ? will it give error?
how to avoid any errors and do dis successfully?
r937 — 2013-07-16T10:51:47-04:00 — #2
the database engine will never execute them at the same time
one will go ahead, the other will wait until the one is finished
by the way, query inside a foreach loop is inefficient, there's usually a better way
coolguy010 — 2013-07-16T11:11:28-04:00 — #3
actually im trying to do my task in parallel in order to increase speed...
d sql part, i have put some code inside a function, [in between these quries come]...
and im calling the function via foreach loop to do the work in parallel..
and r u sure about it?
bcause this table contains some pattern,
>when finds a pattern, it first searches the table if that pattern already exist, only when its not present it runs the insertion query and inserts that pattern...
r937 — 2013-07-16T11:23:45-04:00 — #4
so you're doing a SELECT and then an INSERT, right?
that's two sql calls
better to do just one sql call, and you improve performance by 100%
do the INSERT, and let the database tell you when it fails due to duplicate key
coolguy010 — 2013-07-16T13:00:12-04:00 — #5
ya i thought somwat similar but will it not cause any error when it encounters a duplicate key ???
and secondly i am doin insert because if the pattern exists in the table then, it will that particular records pattern no and store it in a var, this info will be used in the later part of the code ..
>and wat were u telling abt the foreach loop, is it ok considering the situation ?
r937 — 2013-07-16T13:08:20-04:00 — #6
yes, of course
your application code would catch the error from the database, and generate a user-friendly error message exactly like the error message you currently produce when you do the SELECT and find the entry that you want to insert already exists
coolguy010 — 2013-07-16T13:22:37-04:00 — #7
and how can we achieve that wen coding ?
im using php, mysql..
u didnt tell about the foreach loop..
r937 — 2013-07-16T13:33:18-04:00 — #8
i dunno, man, i don't do php
you should not do it if possible
felgall — 2013-07-16T15:33:20-04:00 — #9
The place to start is with the SQL for the insert.
After the call the first thing you need to test for is if the SQL failed with a duplicate key.
Rearrange the way that the code works to do away with the inefficient foreach. If you do need a loop then move the SQL calls outside the loop so that the loop is only running on PHP variables that have already been loaded from the database call.
coolguy010 — 2013-07-16T19:49:11-04:00 — #10
the foreach is not being used now.. there is only one foreach now, which takes the contents frm array,..
however the sql is inside a few for loops, where i dnt think most of the loops can be avoided..
anyway thanks a lot guys..