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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:53:45+00:00 2026-06-01T00:53:45+00:00

I wrote this query: INSERT INTO KeysTable (KeyText, Id) SELECT KeyText as BKT, KeysTable.ID

  • 0

I wrote this query:

INSERT INTO KeysTable (KeyText, Id)
SELECT KeyText as BKT, KeysTable.ID as CID FROM KeysTable
INNER JOIN StatTable ON KeysTable.ID = StatTable.Key_ID
WHERE StatTable.StatCommandCode = 4 AND 
EXISTS (SELECT 1 FROM StatTable WHERE StatCommandCode = 4 AND StatTable.Key_ID = CID);

I know that removing the condition

AND StatTable.Key_ID = CID

would make the query very fast. Also if I replace it with

AND StatTable.Key_ID = 444 // (444 - random static number)

the query will be very fast too. Both the columns in this condition are indexed:

CREATE INDEX IF NOT EXISTS StatsIndex ON StatTable (Key_ID);

and in KeysTable the ID column is primary key. Why doesn’t the index improve perfomance in this case?

Thanks for answers and sorry for my bad english :(.

  • 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-01T00:53:46+00:00Added an answer on June 1, 2026 at 12:53 am

    If there is no CID column in any of the two tables, then the EXISTS subquery is useless. Rewrite the statement as:

    INSERT INTO KeysTable (KeyText, Id)
      SELECT KeyText
           , KeysTable.ID  
      FROM KeysTable
        INNER JOIN StatTable 
          ON KeysTable.ID = StatTable.Key_ID
      WHERE StatTable.StatCommandCode = 4 
    

    If it is still slow, you can try adding an index on (StatCommandCode, Key_ID)

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

Sidebar

Related Questions

I wrote this simple query statement: INSERT INTO merchants ('firstName','lastName') VALUES ('Bob','Smith') Sounds very
I wrote this query in php: $query = insert into admin values($user,$pass,$email,$signature); mysql_query($query); But
I want to write a query like this: SELECT o.OrderId, MAX(o.NegotiatedPrice, o.SuggestedPrice) FROM Order
I am running this sort of query: insert into mytable (id, col1, col2) values
I'm trying to make this information insert into my table in mysql database with
I wrote a simple class: @Entity @Table(name = battle_log) @SQLInsert(sql = INSERT INTO battle_log
In TSQL I can state: insert into myTable (ID) values (5) GO select *
Im trying to write this script to say if email is valied insert into
if for example this query returns a table name called invoice : select table_name
I'm learning to put values into my db from php. this is my simple

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.