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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:34:18+00:00 2026-06-09T07:34:18+00:00

Here is my query: SELECT COUNT(`id`) FROM `comments` WHERE `user_id` != ‘1’ — Author

  • 0

Here is my query:

SELECT COUNT(`id`)
FROM `comments`
WHERE
    `user_id` != '1' -- Author does not need to know about his own comments
    AND (
        `article_id` IN (4, 2476, 14954, 83981, 83982)
        OR `request_id` IN (3, 5, 7)
    );

And the table:

CREATE TABLE `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `part_id` int(11) DEFAULT NULL,
  `article_id` int(11) DEFAULT NULL,
  `request_id` int(11) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `name` varchar(500) DEFAULT NULL,
  `comment` text,
  `add_date` int(11) DEFAULT NULL,
  `ip` text,
  PRIMARY KEY (`id`),
  KEY `key_article_id_add_date` (`article_id`,`add_date`),
  KEY `key_part_id_add_date` (`part_id`,`add_date`),
  KEY `add_date` (`add_date`),
  KEY `request_id` (`request_id`),
  KEY `user_id` (`user_id`,`article_id`,`request_id`)
) ENGINE=innodb AUTO_INCREMENT=3536452 DEFAULT CHARSET=utf8

No matter how I try and what index I add, mysql perferrs to scan all 3039138 rows and query takes forewer. Here is the EXPLAIN output:

+----+-------------+----------+------+--------------------------------------------+------+---------+------+---------+-------------+
| id | select_type | table    | type | possible_keys                              | key  | key_len | ref  | rows    | Extra       |
+----+-------------+----------+------+--------------------------------------------+------+---------+------+---------+-------------+
|  1 | SIMPLE      | comments | ALL  | key_article_id_add_date,user_id,request_id | NULL | NULL    | NULL | 3039138 | Using where |
+----+-------------+----------+------+--------------------------------------------+------+---------+------+---------+-------------+

If I remove one of confitions (user_id, article_id or request_id) mysql seems to have no problems using indexes, but with all the 3 of them – nothing helps.

Any ideas how I can index comments table so mysql could use an index on this query? Or is there any other solution to improve this query performance.

  • 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-09T07:34:20+00:00Added an answer on June 9, 2026 at 7:34 am

    First check the current execution plan for this query and identify whether its using the key you have created for those three columns. If it is not using that particular index, then force the sql to use that index like below

    SELECT Count(`id`)
    FROM   `comments` USE INDEX (user_id_corresponding_index_name)
    WHERE  `user_id` != '1'
           -- Author does not need to know about his own comments    
           AND ( `article_id` IN ( 4, 2476, 14954, 83981, 83982 )
                  OR `request_id` IN ( 3, 5, 7 ) );
    

    Please replace user_id_corresponding_idex_name with corresponding name of non-clustered index created for user_id key in your table.

    http://dev.mysql.com/doc/refman/4.1/en/index-hints.html

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

Sidebar

Related Questions

Here is my query: SELECT DISTINCT `c`.`user_id`, `c`.`created_at`, `c`.`body`, (SELECT COUNT(*) FROM profiles_comments c2
Here is my sample query: SELECT userid,count(*) FROM hits GROUP BY userid My table
here is the query SELECT * FROM customers WHERE NOT EXISTS ( SELECT 1
Here's my query: SELECT solGroup,; SUM(IIF((; SELECT COUNT(*) FROM cgift c2; WHERE c2.solgroup !=
Here's the query: SELECT COUNT(*) AS c, MAX(`followers_count`) AS max_fc, MIN(`followers_count`) AS min_fc, MAX(`following_count`)
Here's my query SELECT * FROM wp_postmeta WHERE meta_value LIKE '.$_POST['username'].' AND meta_value LIKE
Here is my query Select Gender from Table The result pane shows 1 1
Here is my Query : select EmployeeName, EmployeeSalary from Employee2 for xml path('EmployeeDetails') returns
Here is my query select t1.*, t2.name as customer, t2.name as vendor from order
Here is my query select * from table1 inner join table2 on table1.typeId=table2.typeId; This

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.