Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 620903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:48:56+00:00 2026-05-13T18:48:56+00:00

What’s a good way to work with many rows in MySql, given that I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:48:57+00:00Added an answer on May 13, 2026 at 6:48 pm

    I would use the temp table solution, and join it to each main table in the DELETE statements. So you only have to do nine deletes, one for each table.

    • CREATE TEMPORARY TABLE

      CREATE TEMPORARY TABLE Keys (
          Key1 INT UNSIGNED NOT NULL, 
          Key2 INT UNSIGNED NOT NULL, 
          PRIMARY KEY(Key1, Key2)
      );
      
    • Load a file of tab-separated data into the temp table using LOAD DATA LOCAL INFILE

      LOAD DATA LOCAL INFILE 'C:/path/to/datafile' INTO TABLE Keys;
      
    • Delete using MySQL’s multi-table DELETE syntax.

      DELETE t FROM Table1 t JOIN Keys USING (Key1, Key2);
      DELETE t FROM Table2 t JOIN Keys USING (Key1, Key2);
      DELETE t FROM Table3 t JOIN Keys USING (Key1, Key2);
      DELETE t FROM Table4 t JOIN Keys USING (Key1, Key2);
      DELETE t FROM Table5 t JOIN Keys USING (Key1, Key2);
      DELETE t FROM Table6 t JOIN Keys USING (Key1, Key2);
      DELETE t FROM Table7 t JOIN Keys USING (Key1, Key2);
      DELETE t FROM Table8 t JOIN Keys USING (Key1, Key2);
      DELETE t FROM Table9 t JOIN Keys USING (Key1, Key2);
      

    Re your comment:

    The MySQL docs on CREATE TABLE say the following:

    A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.)

    That’s pretty clear!

    Regarding loading the data, you could just do it with INSERT. 5000 rows is no big deal. I use a PHP script to load millions of rows (e.g. the StackOverflow XML data dump) into MySQL and that only takes about 20 minutes. I use a prepared statement and then execute it with parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.