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

  • Home
  • SEARCH
  • 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 7844817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:00:57+00:00 2026-06-02T17:00:57+00:00

I have this query, which was working fine. SELECT SUM(amount) FROM company.invoice_line WHERE item_id

  • 0

I have this query, which was working fine.

SELECT SUM(amount) FROM company.invoice_line WHERE item_id != shipping 
    AND item_id != '' 
    AND invoice_id IN 
        (SELECT id_invoices FROM company.invoices WHERE customer = 'XX' 
            AND sales_rep = 'XXX');

The purpose is to sum up all the rep’s sales from a customer. The customer and rep data are related and stored in the invoice table, and the invoice line table related to the invoice table.

For the data sizes I am working with, the query takes about 0.015s

I changed the id_invoices in the query with another PK that was a VARCHAR, but not flagged unique or what not.

The reason was before hand, I had an atrocious design where an invoice would be inserted into the DB, then a query would immediately follow asking the invoice‘s auto-incremented PK for use as a foreign key.

To use BULK INSERT effectively, I needed access to unique identifiers for practically all my data without depending on an auto-incremented, ‘vanilla’ INT PK. I accomplished this by doing as I said above, and adding additional columns that would serve as foreign keys, etc.

My insert rate is now fantastic, but now the query takes 7+ seconds.

To reiterate, before hand I was using vanilla auto-increment int as a PK. Does switching foreign keys to VARCHARs really destroy performance that much?

My next move appears to be reverting to the int id’s, but instead of allowing MySQL to auto-increment on insert, to manually create these int indexes before hand so I can still use bulk insert. From the perspective of the query, it shouldn’t matter… should it?

Any help would be appreciated.

Dane

  • 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-06-02T17:01:00+00:00Added an answer on June 2, 2026 at 5:01 pm

    Okay, firstly you need to use EXPLAIN to determine what’s happening in the query plan to see what else may have changed.

    Secondly, VARCHAR columns are slower to match than INT columns, though generally it’s just a constant increase (eg it’s k*O(n) vs O(n) where k is not related to n). ….EXCEPT if the character sets are different on the two tables. Then it becomes a huge problem as MySQL tries to match two different character sets. Who knows why, it’s just slow mmkay.

    Thirdly, were your inserts really that slow as to require this massive redesign? It’s not clear from your question what you’re doing, but it’s hard to see how the performance on random inserts was that big a hit on your workload that you needed to make a very nonstandard table structure which makes everything else harder and slower to work around it?

    Finally, your last question about bulk inserts – if you precreate the rows, insert isn’t going to work (unless you do something with ON DUPLICATE KEY). But I’d always try and stick to int IDs for this sort of thing unless there’s a very good reason not to.

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

Sidebar

Related Questions

I have a query which looks like this: SELECT LossCost, CoverageID FROM BGILossCost] WHERE
I have this query which groups the results by ORDER#. SELECT ORDER#, MAX(SHIPDATE -
I have this mysql query using PHP and first part is working fine, but
I have the following dynamic query which is working fine without the WHERE clause,
I have this query which on executing in my sql command line client executes
I have this query which is a dependant query and taking much execution time
I have this query which works correctly in MySQL. More background on it here
I have this query which returns a result set with Date(column) as nvarchar datatype.
Right now I have this SQL query which is valid but always times out:
I have this query, which is giving me some problems... I am trying to

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.