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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:18:48+00:00 2026-06-02T20:18:48+00:00

Is there any way to optimize this query? It takes more than 2.5 secs.

  • 0

Is there any way to optimize this query? It takes more than 2.5 secs.

SELECT articles.categories_id,
    COUNT(articles.id) AS count
FROM articles
WHERE articles.created >= DATE_SUB(NOW(), INTERVAL 48 HOUR)
GROUP BY articles.categories_id
ORDER BY count DESC

CREATE TABLE IF NOT EXISTS `articles` (
  `id` int(11) NOT NULL,
  `categories_id` int(11) NOT NULL,
  `feeds_id` int(11) NOT NULL DEFAULT '0',
  `users_id` int(11) NOT NULL DEFAULT '1',
  `title` varchar(255) CHARACTER SET utf8 NOT NULL,
  `sefriendly` varchar(255) CHARACTER SET utf8 NOT NULL,
  `body` text CHARACTER SET utf8,
  `source` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  `created` datetime NOT NULL,
  `edited` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `fingerprint` varchar(32) CHARACTER SET utf8 NOT NULL,
  `type` int(1) NOT NULL DEFAULT '1' COMMENT '1 => Feed fetched article\n2 => User submitted article',
  `description` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  `keywords` text CHARACTER SET utf8,
  `status` int(1) NOT NULL DEFAULT '0' COMMENT '0 => Passive\n1 => Active\n2 => Pending',
  PRIMARY KEY (`id`),
  KEY `categories_id` (`categories_id`) USING BTREE,
  KEY `feeds_id` (`feeds_id`) USING BTREE,
  KEY `users_id` (`users_id`) USING BTREE,
  KEY `fingerprint` (`fingerprint`) USING BTREE,
  KEY `title` (`title`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci ROW_FORMAT=COMPACT;

I already use caching, so there is no problem in terms of code.

This is the explain sql result:

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  articles    index   NULL    categories_id   4   NULL    120411  Using where; Using temporary; Using file sort

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-02T20:18:51+00:00Added an answer on June 2, 2026 at 8:18 pm

    You can improve things by adding an index on created. This will help serve your WHERE clause:

    WHERE articles.created >= DATE_SUB(NOW(), INTERVAL 48 HOUR)
    

    It will probably be advantageous to instead create a covering index on (created, categories_id) so that all the data required for the query is available in the index.

    Note that the value of id is not needed for this query because COUNT only cares if the value is NULL or NOT NULL, but id is defined to be NOT NULL in your table definition. It would probably be a good idea to make this explicit by using COUNT(*) or COUNT(1) instead of COUNT(id) as this is guaranteed to give the same result. But I would expect that MySQL is intelligent enough to make this optimization for you automatically.

    I don’t think that you can avoid the file sort because you are sorting on the result of an aggregation, and this cannot be indexed.

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

Sidebar

Related Questions

I'm wondering if there's any way to optimize the following SELECT query. (Note: I
Is there any way I can avoid using array_flip to optimize performance. I am
I would need some help on how to optimize the query. select * from
This is my query: SELECT B.RECORDID, A.ITEMCODE, A.ITEMNAME, A.STOCKINHAND, B.SALEPRICE FROM ITEMMASTER A, STOCKENTRY
Is there any way to prevent ActiveRecord from issued a SHOW FIELDS to the
Apologies if this has been asked before but is there any way, at all,
I wonder if there is any wise way to rewrite the following query so
Is there any way to update cookies across folders in PHP? For example, consider
Is there any way to trigger a function that's called when the user types
:) Is there any way to create an index, and incrementing with a given

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.