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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:15:00+00:00 2026-06-13T00:15:00+00:00

I have a table with 40,000,000 rows, and I’m trying to optimize my query,

  • 0

I have a table with 40,000,000 rows, and I’m trying to optimize my query, because takes too long.
First, this is my table:
CREATE TABLE resume (
yearMonth char(6) DEFAULT NULL,
type char(1) DEFAULT NULL,
agen_o char(5) DEFAULT NULL,
tar char(2) DEFAULT NULL,
cve_ent char(1) DEFAULT NULL,
cve_mun char(3) DEFAULT NULL,
cve_reg int(1) DEFAULT NULL,
id_ope char(1) DEFAULT NULL,
ope_tip char(2) DEFAULT NULL,
ope_cve char(3) DEFAULT NULL,
cob_u int(9) DEFAULT NULL,
tot_imp bigint(15) DEFAULT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

This is my query:
SELECT m.name_ope AS cve,
SUBSTRING(r.yearMonth,5,2) AS period,
COUNT(DISTINCT(CONCAT(r.agen_ope,r.cve_ope))) AS num,
SUM(CASE WHEN r.type=’A’ THEN r.cob_u ELSE 0 END) AS tot_u,
FROM resume r, media m
WHERE CONCAT(r.id_ope,SUBSTRING(r.ope_cve,3,1))=m.ope_cve AND
r.type IN (‘C’,’D’,’E’) AND
SUBSTRING(r.yearMonth,1,4)=’2012′ AND
r.id_ope=’X’ AND
SUBSTRING(r.ope_cve,1,2) IN (SELECT cve_med FROM catNac WHERE numero=’0′)
GROUP BY SUBSTRING(r.yearMonth,5,2),SUBSTRING(r.ope_cve,3,1)
ORDER BY SUBSTRING(r.yearMonth,5,2),SUBSTRING(r.ope_cve,3,1)

So, I added an index with these fields: id_ope, yearMonth, agen_o, because I have other’s queries that have this fields in WHERE, with this order

Now my explain output:
1 PRIMARY r ref indice indice 2 const 14774607 Using where; Using filesort

So i added another index with yearMonth, ope_cve, but I still have “using filesort”. How can I optimize this?
Thanks

  • 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-13T00:15:00+00:00Added an answer on June 13, 2026 at 12:15 am

    Without modifying your table structure, if you have an index on yearMonth, you can try this:

    SELECT m.name_ope AS cve,
    SUBSTRING(r.yearMonth,5,2) AS period,
    COUNT(DISTINCT(CONCAT(r.agen_ope,r.cve_ope))) AS num,
    SUM(CASE WHEN r.type='A' THEN r.cob_u ELSE 0 END) AS tot_u,
    FROM resume r, media m
    WHERE CONCAT(r.id_ope,SUBSTRING(r.ope_cve,3,1))=m.ope_cve AND
    r.type IN ('C','D','E') AND
    r.yearMonth LIKE '2012%' AND
    r.id_ope='X' AND
    SUBSTRING(r.ope_cve,1,2) IN (SELECT cve_med FROM catNac WHERE numero='0')
    GROUP BY r.yearMonth,SUBSTRING(r.ope_cve,3,1)
    

    The changes:

    • Using r.yearMonth LIKE '2012%' should allow an index to be used for that part of your where clause.
    • Since you’re already filtering out every year but 2012, you can group by GROUP BY r.yearMonth alone.
    • The ORDER BY clause is not needed since MySQL sorts on GROUP BY, unless you include ORDER BY NULL
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is staight forward I believe: I have a table with 30,000 rows. When
I have a table with more than 10 000 000 rows. In TOAD this
I have a table with ~100,000 rows. I am trying to print to screen
Guys I'm stumped on this one, I have a large table with 100,000 rows
I have a big table (200'000'000 rows); declared like this thread( forum_id tinyint, thread_id
I have a simple table with 1,000,000 rows. this row has a datetime field
I have a table with about 2,000,000 rows. I need to query one of
I have table with around 70 000 rows. There is 6000 rows that i
I have a table with about 5,000 rows which I build dynamically with jQyuery.
I have a table (session) in a database which has almost 72,000 rows. I

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.