What happens if Simultaneous Insertion query with same table

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?

Thanks

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

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…

Thanks

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

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 ?

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

and how can we achieve that wen coding ?
im using php, mysql…
u didnt tell about the foreach loop…

i dunno, man, i don’t do php

you should not do it if possible

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.

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…