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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:53:26+00:00 2026-05-20T15:53:26+00:00

I have this query: SELECT p.prodno AS id, proddesc AS label FROM product p

  • 0

I have this query:

SELECT p.prodno AS id, 
       proddesc AS label 
FROM   product p 
       JOIN sales s 
         ON s.custno = 00800 
            AND s.deptno = 0 
            AND s.prodno = p.prodno 
GROUP  BY p.prodno 
ORDER  BY p.prodno ASC 

Explain returns this:

+---+-----------+------+--------+------------------------------------------------------------------------------+------------+------+----------------------------------------+------+---------+------------------------------------+
| 1 |  'SIMPLE' |  'p' |  'ALL' |  'PRIMARY'                                                                   |  ''        |  ''  |  ''                                    |  481 |  100.00 |  'Using temporary; Using filesort' |
| 1 |  'SIMPLE' |  's' |  'ref' |  'PRIMARY,in_sales_custnodeptnoprodno,in_sales_deptnocustno,in_sales_custno' |  'PRIMARY' |  '6' |  'const,const,bkp_teststats2.p.PRODNO' |   93 |  100.00 |  'Using index'                     |
+---+-----------+------+--------+------------------------------------------------------------------------------+------------+------+----------------------------------------+------+---------+------------------------------------+

As you see there is no index used in the first row for PRODNO, but the table schema has index on it.

CREATE TABLE IF NOT EXISTS `product` (
  `PRODNO` decimal(4,0) unsigned zerofill NOT NULL DEFAULT '0000',
  `PRODDESC` char(21) NOT NULL DEFAULT '',
  `UPCCODE12` decimal(12,0) unsigned zerofill NOT NULL DEFAULT '000000000000',
  PRIMARY KEY (`PRODNO`)
)

And sales has these keys:

PRIMARY KEY (`CUSTNO`,`DEPTNO`,`PRODNO`,`ARDATE8N`),
KEY `in_sales_custnodeptnoprodno` (`CUSTNO`,`DEPTNO`,`PRODNO`),
KEY `in_sales_deptnocustno` (`DEPTNO`,`CUSTNO`),
KEY `in_sales_custno` (`CUSTNO`),

I would like to drop Using temporary; Using filesort because the above query takes 14 seconds on a 50G data table.

Update:

Problem: I want to get a unique product list that have sales data for a given custno and deptno.

  • 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-05-20T15:53:26+00:00Added an answer on May 20, 2026 at 3:53 pm

    The database has to check every row in the product table to satisfy your query. If it used the index, it would have to go back to the main table for every row to pick up proddesc. Going back to the main table (a “bookmark lookup”) is quite expensive. So the query optimizer chooses to scan and sort the main table, which seems like a good choice to me.

    If you omit proddesc from the result, the query only requires prodno. In that case the query optimizer will probably use the index.

    You could also expand the index on products from (prodno) to (prodno, proddesc). Expanded this way, the index can satisfy the query without table lookups.

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

Sidebar

Related Questions

I have this Query: SELECT p.ProductName, dt.MaxTimeStamp, p.Responsible FROM Product p LEFT JOIN (SELECT
I have this query: SELECT DISTINCT id, label FROM bbproduct_cust WHERE CUSTNO IN (SELECT
I have this query: SELECT groups.name categories.name, categories.label FROM groups JOIN categories ON (categories.group1
I have this query: SELECT * FROM sample INNER JOIN test ON sample.sample_number =
I have this query: SELECT DISTINCT IM.EDIFICIOS_ID, TI.TITULAR FROM IMPORTACION IM INNER JOIN I_EDIFICIO
I have this Query: Select DFC_NOMBRE_CAMPO, DFC_TITULO, TDT_DATO_MOTOR, DFC_LONGITUD FROM MEM_DEFINICIONES_CAMPOS JOIN MEM_TIPOS_DATOS ON
I have this query: SELECT g.title, g.asin, g.platform_id, r.rank FROM games g INNER JOIN
I have this query SELECT articles.*, users.username AS `user` FROM `articles` LEFT JOIN `users`
I have this query: select top(2) property_id_ref ,image_file ,property_name from property_master a inner join
I have this query SELECT *, bp.product_id FROM builder_product as bp JOIN builder_step as

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.