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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:28:00+00:00 2026-05-18T02:28:00+00:00

I need some help optimizing a MySQL query or table When I run this

  • 0

I need some help optimizing a MySQL query or table

When I run this query it return in .01s with 650 records:

  select mm, name, display, year
  from tbl d
  where active = 1 and tbl2_id = 'val' and lvl_id = 9
  order by mm;

When I run this query it return in over 15s with the same records:

  select d.mm, d.name, d.display, d.year, a.year year2
  from tbl d left join tbl a on d.mm = a.mm and a.tbl2_id = 'val2'
  where d.active = 1 and d.tbl2_id = 'val' and d.lvl_id = 9
  order by d.mm;

When I run it like this it also take over 15s:

  select mm, name, display, year, 
      (select a.year from tbl a where a.mm = mm and a.tbl2_id = 'val2') year2
  from tbl 
  where active = 1 and tbl2_id = 'val' and lvl_id = 9
  order by mm;

the table has multiple records for mm. I need to get all records where tbl2_id = ‘val’ and if there is a record for that mm where tbl2_id = ‘val2’, I need the value of “year” from the val2 record. tbl currently has 13k records in it and there are no more then 10 records for any given mm so I don’t think think this query should be taking over 15s. I have indexes mm, active, tbl2_id and lvl_id.

I’ve done similar things in MSSQL with almost no delay.

  • 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-18T02:28:01+00:00Added an answer on May 18, 2026 at 2:28 am

    You could start by introducing composite indexes on your table on (tbl2_id, lvl_id, active) and (tbl2_id, mm). That would likely speed up all three of your queries.

    Whenever you use multiple fields in a WHERE clause, it makes sense to consider a composite index. In case only single-column indexes are present, the query can use only one of those indexes for a seek while having to resort to a slower scan to search the remaining subset. The difference between MySQL and MSSQL in this respect might be that MSSQL makes a better guess which of those three to use based on the cardinality of your data (the best one to use would be the one that leaves the smallest subset for the scan), although this is hard to say without examining the two query plans in detail.

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

Sidebar

Related Questions

Need some help with putting this query together. I'm using Mysql I have two
Need some help with a query.. I have three tables. Source id name 1
Need some help. I have a table with some columns..like name , phone etc...
I need help with optimizing some of my SQL queries. I'm not good in
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
I need some help optimizing queries on 2 tables. One will contain about a
Need some help with what is probably a pretty basic SQL query. I'm trying
Need some help with this error. Fresh wordpress 2.9 install... Fatal error: Cannot instantiate

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.