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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:11:57+00:00 2026-05-11T07:11:57+00:00

I’m trying to figure out how to optimize a very slow query in MySQL

  • 0

I’m trying to figure out how to optimize a very slow query in MySQL (I didn’t design this):

SELECT COUNT(*) FROM change_event me WHERE change_event_id > '1212281603783391'; +----------+ | COUNT(*) | +----------+ |  3224022 | +----------+ 1 row in set (1 min 0.16 sec) 

Comparing that to a full count:

select count(*) from change_event; +----------+ | count(*) | +----------+ |  6069102 | +----------+ 1 row in set (4.21 sec) 

The explain statement doesn’t help me here:

 explain SELECT COUNT(*) FROM change_event me WHERE change_event_id > '1212281603783391'\G *************************** 1. row ***************************            id: 1   select_type: SIMPLE         table: me          type: range possible_keys: PRIMARY           key: PRIMARY       key_len: 8           ref: NULL          rows: 4120213         Extra: Using where; Using index 1 row in set (0.00 sec) 

OK, it still thinks it needs roughly 4 million entries to count, but I could count lines in a file faster than that! I don’t understand why MySQL is taking this long.

Here’s the table definition:

CREATE TABLE `change_event` (   `change_event_id` bigint(20) NOT NULL default '0',   `timestamp` datetime NOT NULL,   `change_type` enum('create','update','delete','noop') default NULL,   `changed_object_type` enum('Brand','Broadcast','Episode','OnDemand') NOT NULL,   `changed_object_id` varchar(255) default NULL,   `changed_object_modified` datetime NOT NULL default '1000-01-01 00:00:00',   `modified` datetime NOT NULL default '1000-01-01 00:00:00',   `created` datetime NOT NULL default '1000-01-01 00:00:00',   `pid` char(15) default NULL,   `episode_pid` char(15) default NULL,   `import_id` int(11) NOT NULL,   `status` enum('success','failure') NOT NULL,   `xml_diff` text,   `node_digest` char(32) default NULL,   PRIMARY KEY  (`change_event_id`),   KEY `idx_change_events_changed_object_id` (`changed_object_id`),   KEY `idx_change_events_episode_pid` (`episode_pid`),   KEY `fk_import_id` (`import_id`),   KEY `idx_change_event_timestamp_ce_id` (`timestamp`,`change_event_id`),   KEY `idx_change_event_status` (`status`),   CONSTRAINT `fk_change_event_import` FOREIGN KEY (`import_id`) REFERENCES `import` (`import_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 

Version:

$ mysql --version mysql  Ver 14.12 Distrib 5.0.37, for pc-solaris2.8 (i386) using readline 5.0 

Is there something obvious I’m missing? (Yes, I’ve already tried ‘SELECT COUNT(change_event_id)’, but there’s no performance difference).

  • 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. 2026-05-11T07:11:58+00:00Added an answer on May 11, 2026 at 7:11 am

    InnoDB uses clustered primary keys, so the primary key is stored along with the row in the data pages, not in separate index pages. In order to do a range scan you still have to scan through all of the potentially wide rows in data pages; note that this table contains a TEXT column.

    Two things I would try:

    1. run optimize table. This will ensure that the data pages are physically stored in sorted order. This could conceivably speed up a range scan on a clustered primary key.
    2. create an additional non-primary index on just the change_event_id column. This will store a copy of that column in index pages which be much faster to scan. After creating it, check the explain plan to make sure it’s using the new index.

    (you also probably want to make the change_event_id column bigint unsigned if it’s incrementing from zero)

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

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.