I'm brought in as a consultant to help modify a 3rd party real estate system to take data directly from MLS FTP pushes. Okay, no big deal...just writing an import into whatever layout this database is in, right?
Ignoring the mostly horrible database layout, I do this. I added a few indexes to increase search speed from 7 sec to .02 and all is good...with the test data of 50 records anyway.
So then the first MLS push comes. 10,000 records. No problem whatsoever with the import. 10,000 properties are in the database and due to their horrible layout, the "fields" table now has 3 million records (roughly 30 per property). Cool, the import worked. My work here is done!
Not quite...
I get a call saying that the advanced search keeps timing out. This isn't what I was hired to do, but considering what the rest of their code and database was like I figured I could fix it in a few minutes.
So I put an echo on every SQL statement running through the system...it's still in testing anyway.
I get 3 MILLION "UPDATE mos_hp_temp SET ...=..." statements on the screen. Actually I only got 40,000 because the script timed out after that.
This is only the start of the problems...
- The system starts out by dropping the temp table. Note that it's not actually a temp table, just a table called "temp".
- It then re-creates this temp table exactly like it was before. But now it's empty!
- Okay, so user B now has an empty table where his search results will go. Great! Anyone stop to think what happened to user A who did a search 30 seconds earlier??? Oh look...user A now has user B's search results...or at least he will in 20 minutes...keep reading.
- Anyway...The code continues on by copying all the properties, agents and companies out of the 3 tables they are nicely stored in and puts them into this one HUGE "temp" table. All of the records mind you, not just the ones you searched for.
- Remember how I said each extra field was it's own row in another table? Well the script loops through this table and performs an update on every single row in that table to update the data in the temp table. Works great if you only have 5 records. When you have 3 million.....
- So now, 15 minutes later, we have an exact copy of our ENTIRE DATABASE, except it is all in one table.
- Wait, didn't they search for something? Well let's go through each search criteria they put in and delete any records from the temp table that don't match it! ONE AT A TIME!