I’m coding PHP kohana application and in one part of it I need to insert a few records into table.
My table structure:
TABLE BOOKINGS
id int
booked_object_id int
date_from date
date_tom date
This table has about 10k records with booked objects.
Now in one of my PHP function I have array with new data, which I want to insert into this table. There are about 20-30 new bookings to insert when my function is executed.
Now let’s get to my main problem – I want it to check if ANY of bookings which I want to insert into table is already booked (object_id and date_from is the same as in my php array). If any of it is already booked, my function will not insert anything new into table.
Is there a more efficient way than getting all contents of mysql table into PHP array and check it 20 times (or number of my inserts)?
If you are planning on looking up a certain column in your table then you should index it.
This will create a hashtable that the db-server will use to lookup rows.
And as for the efficiency of making 20 calls.. you could use an
INstatement to select all existing records in 1 call and simply ignore the already existing ones when inserting.reference:
expr IN (value,…)