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 941267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:05:36+00:00 2026-05-15T22:05:36+00:00

I was wondering if it was possible to speed up this MySQL query. Currently,

  • 0

I was wondering if it was possible to speed up this MySQL query. Currently, I am calling it 20,000+ times, and it takes a while to run (a while being about 10 to 20 minutes).

Here is the basic table layout:

db1:
INT(11) id
VARCHAR(45) col1
VARCHAR(100) col2
VARCHAR(100) col3

db2:
VARCHAR(45) id

db3:
VARCHAR(45) fk_db2
INT(11) fk_db1

Here is the stored routine:

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `proc`(IN fk VARCHAR(45),
                                            IN a VARCHAR(45),
                                            IN b VARCHAR(100),
                                            IN c VARCHAR(65))
BEGIN
    SET @id=0;
    SELECT id INTO @id FROM db1 WHERE db1.col1=a
                                AND db1.col2=b
                                AND db1.col3=c;
    IF @id=0 THEN
        INSERT INTO db1 (col1, col2, col3)
            VALUES (a, b, c);
        SELECT LAST_INSERT_ID() INTO @id;
    END IF;

    -- Association table for db2 and db1.
    INSERT IGNORE INTO db3 (fk_db1, fk_db2) 
        VALUES(@id, fk);
END

The main point of this routine, is I want to get the ID of a specific record, or create one if it doesn’t exist. Then I want to associate the fk passed in and the ID that I just found out. I’m sure there is a MySQL one-liner for this, but I have been unable to figure it out.

Any help is greatly appreciated!!

Thank you!

By the way, the names of columns are much better in the actual database, but I can’t share the names with you all.

  • 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-15T22:05:37+00:00Added an answer on May 15, 2026 at 10:05 pm

    Actually, unless I’m missing something, that doesn’t look all that slow. Can you define “a while” in “takes a while”?

    Do you have an index on col1/col2/col3?

    How exactly are you calling the procedure in your application? You are reusing the database connections, right?

    Can you bundle multiple calls into a single transaction if you’re not already?

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

Sidebar

Related Questions

Just wondering is it possible to build CLI app that can be run from
Im wondering about speed optimization in PHP. I have a series of files that
I was wondering if itwould be possible to somehow speed up symfony templates by
I was wondering ( if possible ) if there was a program/tool/utility that when
Just toying with the SDK and I was wondering if possible a UITouch event
I am wondering if its possible to have a an (||)or or (&&)and operator
I was wondering if its possible to directly cast objects within a foreach loop.
I was wondering if its possible to use the Google maps IFRAME code to
I was wondering - is it possible to change files that are stored on
I was wondering is it possible to register a broadcast receiver to receive two

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.