What's a good way to work with many rows in MySql, given that I have a long list of keys in a client application that is connecting with ODBC?
Note: my experience is largely SQL Server, so I know a bit, just not MySQL specifically.
The task is to delete some rows from 9 tables, but I might have upwards of 5,000 key pairs.
I started out with the easy way of looping through all my keys and submitting a statement for each one against each table, such as:
DELETE FROM Table WHERE Key1 = 123 AND Key2 = 567 -- and 8 more tables
DELETE FROM Table WHERE Key1 = 124 AND Key2 = 568 -- and 8 more tables
DELETE FROM Table WHERE Key1 = 125 AND Key2 = 569 -- and 8 more tables
...
Except, that comes out to 45,000 separate statements, which as you can imagine is a bit slow.
So, without worrying about the programming language I'm using on the front end, what's a good way to submit the list so that I can JOIN and do the operation all at once or at least in large batches? Here are my ideas so far:
Create a temp table and insert to it, then join. I'll happily look up the syntax for MySQL to create a temp table, but is that a good route to go?
Assuming I do use a temp table, what's the best method for populating a temp table? 5000
INSERT Table VALUES ()
statements?SELECT 123, 456 UNION ALL SELECT 124, 457
? I just tested that MySql allows this kind of SELECT that is not issued against a table. But SQL Server eventually blows up if the list gets too long, so is this a good way in MySQL? Should I just keep the list to a few hundred at once?--CREATE Temp Table ( I do not know the syntax in MySql yet) INSERT INTO TempTable SELECT 123, 456 UNION ALL SELECT 124, 457 UNION ALL SELECT 125, 458 DELETE T FROM Table T INNER JOIN TempTable X ON T.Key1 = X.Key1 AND T.Key2 = X.Key2
XML. I see MySQL 5.1 has some XML functions, but from a cursory search it doesn't appear to support turning a chunk of XML text into a rowset to join against. Is that true? It is extremely easy for me to get the values into XML.
A virtual split operation. I presume in MySql that there's some kind of procedural language possible. In SQL Server I could write some custom code that parses a string and turns it into a rowset:
CREATE PROCEDURE DoStuff @KeyString varchar(max) AS DECLARE @Keys TABLE ( Key1 int, Key2 int, PRIMARY KEY CLUSTERED (Key1, Key2) ) DECLARE @Pos int WHILE @Pos < Len(@KeyString) BEGIN -- loop to search for delimiting commas in @KeyString -- and insert pairs of parsed tokens to table variable @Keys END DELETE T FROM Table T INNER JOIN @Keys K ON T.Key1 = K.Key1 AND T.Key2 = K.Key2
Since I'm unfamiliar with MySQL, I really don't know which possibility to investigate first, and I would appreciate some help to save me from making a poor decision and/or learning the hard way.