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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:54:18+00:00 2026-06-15T14:54:18+00:00

Table structure: CREATE TABLE IF NOT EXISTS `logs` ( `id` bigint(20) unsigned NOT NULL

  • 0

Table structure:

CREATE TABLE IF NOT EXISTS `logs` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user` bigint(20) unsigned NOT NULL,
  `type` tinyint(1) unsigned NOT NULL,
  `date` int(11) unsigned NOT NULL,
  `plus` decimal(10,2) unsigned NOT NULL,
  `minus` decimal(10,2) unsigned NOT NULL,
  `tax` decimal(10,2) unsigned NOT NULL,
  `item` bigint(20) unsigned NOT NULL,
  `info` char(10) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `item` (`item`),
  KEY `user` (`user`),
  KEY `type` (`type`),
  KEY `date` (`date`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 PACK_KEYS=0 ROW_FORMAT=FIXED;

Query:

SELECT logs.item, COUNT(logs.item) AS total FROM logs WHERE logs.type = 4 GROUP BY logs.item;

Table holds 110k records out of which 50k type 4 records.
Execution time: 0.13 seconds

I know this is fast, but can I make it faster?

I am expecting 1 million records and thus the time would grow quite a bit.

  • 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-15T14:54:19+00:00Added an answer on June 15, 2026 at 2:54 pm

    Analyze queries with EXPLAIN:

    mysql> EXPLAIN SELECT logs.item, COUNT(logs.item) AS total FROM logs 
        WHERE logs.type = 4 GROUP BY logs.item\G
    
               id: 1
      select_type: SIMPLE
            table: logs
             type: ref
    possible_keys: type
              key: type
          key_len: 1
              ref: const
             rows: 1
            Extra: Using where; Using temporary; Using filesort
    

    The “Using temporary; Using filesort” indicates some costly operations. Because the optimizer knows it can’t rely on the rows with each value of item being stored together, it needs to scan the whole table and collect the count per distinct item in a temporary table. Then sort the resulting temp table to produce the result.

    You need an index on the logs table on columns (type, item) in that order. Then the optimizer knows it can leverage the index tree to scan each value of logs.item fully before moving on to the next value. By doing this, it can skip the temporary table to collect values, and skip the implicit sorting of the result.

    mysql> CREATE INDEX logs_type_item ON logs (type,item);
    
    mysql> EXPLAIN SELECT logs.item, COUNT(logs.item) AS total FROM logs 
        WHERE logs.type = 4 GROUP BY logs.item\G
    
               id: 1
      select_type: SIMPLE
            table: logs
             type: ref
    possible_keys: type,logs_type_item
              key: logs_type_item
          key_len: 1
              ref: const
             rows: 1
            Extra: Using where
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQL STRUCTURE CREATE TABLE IF NOT EXISTS `map` ( `id` int(11) NOT NULL AUTO_INCREMENT,
Table Structure CREATE TABLE IF NOT EXISTS `blogs` ( `id` int(11) NOT NULL auto_increment,
Simplified Table structure: CREATE TABLE IF NOT EXISTS `hpa` ( `id` bigint(15) NOT NULL
My table structure is below: CREATE TABLE [ACC].[Document]( [DocumentID] [int] IDENTITY(1,1) NOT NULL, [Date]
My DB Structure: CREATE TABLE IF NOT EXISTS `pos` ( `id` int(11) NOT NULL
My table structure is: CREATE TABLE IF NOT EXISTS `users_settings_temp` ( `id` int(10) unsigned
Given the following table structure: CREATE TABLE IF NOT EXISTS `roles` ( `id` int(11)
I have the following structure: CREATE TABLE IF NOT EXISTS `myTable` ( `ID` int(11)
Here is my DB Structure: CREATE TABLE IF NOT EXISTS `UserItems` ( `id` bigint(20)
I have this database structure, SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; CREATE TABLE IF NOT EXISTS `announces` (

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.