A MySQL EXPLAIN number of rows discrepancyWhy does it use temporary? (MySQL)EXPLAIN output suggests that my...

What is the wife of a henpecked husband called?

Showing size of pie chart in legend of QGIS?

Can I become debt free or should I file for bankruptcy? How do I manage my debt and finances?

Why doesn't "auto ch = unsigned char{'p'}" compile under C++ 17?

Every character has a name - does this lead to too many named characters?

How would a Dictatorship make a country more successful?

How to avoid being sexist when trying to employ someone to function in a very sexist environment?

What kind of hardware implements Fourier transform?

Are there neural networks with very few nodes that decently solve non-trivial problems?

Why is "points exist" not an axiom in geometry?

What to do when being responsible for data protection in your lab, yet advice is ignored?

Disable the ">" operator in Rstudio linux terminal

Contest math problem about crossing out numbers in the table

Checking for the existence of multiple directories

Compress command output by piping to bzip2

Pre-1980's science fiction short story: alien disguised as a woman shot by a gangster, has tentacles coming out of her breasts when remaking her body

How to tag distinct options/entities without giving any an implicit priority or suggested order?

How to deal with an incendiary email that was recalled

figures in a grid with multiple line of texts

Difference between two quite-similar Terminal commands

Program that converts a number to a letter of the alphabet

Would a National Army of mercenaries be a feasible idea?

How can animals be objects of ethics without being subjects as well?

What is better: yes / no radio, or simple checkbox?



A MySQL EXPLAIN number of rows discrepancy


Why does it use temporary? (MySQL)EXPLAIN output suggests that my index is not being usedAre two indexes needed?MySQL optimization - year column grouping - using temporary table, filesortMySQL Indexing VarCharFinding rows for a specified date rangeIdentical query, tables, but different EXPLAIN and performanceDeciding which MySQL execution plan is betterDifference beetween mysql 5.5 and 5.6Optimizing a simple query on a large tableselect MAX() from MySQL view (2x INNER JOIN) is slow













1















MySQL 5.5.49-log



More questions on the query in Why does it use temporary? (MySQL) (the query is the same but the question is different):



I have the following table (filled with many rows):



CREATE TABLE `SectorGraphs2` (
`Kind` tinyint(3) UNSIGNED NOT NULL COMMENT '1 - продюсер, 2 - жанр, 3 - регион',
`Criterion` tinyint(3) UNSIGNED NOT NULL,
`Period` tinyint(3) UNSIGNED NOT NULL,
`PeriodStart` date NOT NULL,
`SectorID` int(10) UNSIGNED NOT NULL,
`Value` float NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

ALTER TABLE `SectorGraphs2`
ADD UNIQUE KEY `Producer2` (`Kind`,`Criterion`,`Period`,`PeriodStart`,`SectorID`) USING BTREE,
ADD KEY `SectorID` (`SectorID`);


then I run:



EXPLAIN 
SELECT SectorID, SUM(Value)
FROM SectorGraphs2
WHERE Kind = 1 AND Criterion = 7
AND Period = 1
AND PeriodStart >= ? AND PeriodStart < ? + INTERVAL 1 WEEK
GROUP BY SectorID


and it produces:



+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra |
+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | SectorGraphs2 | range | Producer2 | Producer2 | 6 | NULL | 1 | Using where; Using temporary; Using filesort |
+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+


See a nicely formatted explanation here.



My question: Why it is used a temporary table and filesort but it reports only 1 row examined? It seems that because of using a temporary table, it should process more than one row. How can I determine the real number of rows processed? How to solve this discrepancy about number of processed rows?



Note that the task I was assigned to do now is to eliminate heavy (involving too many rows) queries. And now I do not know how to do this.










share|improve this question
















bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • As Rick has explain there,"an index cannot handle both a range (PeriodStart...) and a GROUP BY" .If this is your real table and real query then I too am astonished becasue your table only contain int and date so it should be super fast.Hope you are using variable in your query as sugested below.Is estimated and actual number of rows same in plan ?Do you get same plan for other parameter also ?

    – KumarHarsh
    Jun 13 '17 at 6:40
















1















MySQL 5.5.49-log



More questions on the query in Why does it use temporary? (MySQL) (the query is the same but the question is different):



I have the following table (filled with many rows):



CREATE TABLE `SectorGraphs2` (
`Kind` tinyint(3) UNSIGNED NOT NULL COMMENT '1 - продюсер, 2 - жанр, 3 - регион',
`Criterion` tinyint(3) UNSIGNED NOT NULL,
`Period` tinyint(3) UNSIGNED NOT NULL,
`PeriodStart` date NOT NULL,
`SectorID` int(10) UNSIGNED NOT NULL,
`Value` float NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

ALTER TABLE `SectorGraphs2`
ADD UNIQUE KEY `Producer2` (`Kind`,`Criterion`,`Period`,`PeriodStart`,`SectorID`) USING BTREE,
ADD KEY `SectorID` (`SectorID`);


then I run:



EXPLAIN 
SELECT SectorID, SUM(Value)
FROM SectorGraphs2
WHERE Kind = 1 AND Criterion = 7
AND Period = 1
AND PeriodStart >= ? AND PeriodStart < ? + INTERVAL 1 WEEK
GROUP BY SectorID


and it produces:



+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra |
+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | SectorGraphs2 | range | Producer2 | Producer2 | 6 | NULL | 1 | Using where; Using temporary; Using filesort |
+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+


See a nicely formatted explanation here.



My question: Why it is used a temporary table and filesort but it reports only 1 row examined? It seems that because of using a temporary table, it should process more than one row. How can I determine the real number of rows processed? How to solve this discrepancy about number of processed rows?



Note that the task I was assigned to do now is to eliminate heavy (involving too many rows) queries. And now I do not know how to do this.










share|improve this question
















bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • As Rick has explain there,"an index cannot handle both a range (PeriodStart...) and a GROUP BY" .If this is your real table and real query then I too am astonished becasue your table only contain int and date so it should be super fast.Hope you are using variable in your query as sugested below.Is estimated and actual number of rows same in plan ?Do you get same plan for other parameter also ?

    – KumarHarsh
    Jun 13 '17 at 6:40














1












1








1








MySQL 5.5.49-log



More questions on the query in Why does it use temporary? (MySQL) (the query is the same but the question is different):



I have the following table (filled with many rows):



CREATE TABLE `SectorGraphs2` (
`Kind` tinyint(3) UNSIGNED NOT NULL COMMENT '1 - продюсер, 2 - жанр, 3 - регион',
`Criterion` tinyint(3) UNSIGNED NOT NULL,
`Period` tinyint(3) UNSIGNED NOT NULL,
`PeriodStart` date NOT NULL,
`SectorID` int(10) UNSIGNED NOT NULL,
`Value` float NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

ALTER TABLE `SectorGraphs2`
ADD UNIQUE KEY `Producer2` (`Kind`,`Criterion`,`Period`,`PeriodStart`,`SectorID`) USING BTREE,
ADD KEY `SectorID` (`SectorID`);


then I run:



EXPLAIN 
SELECT SectorID, SUM(Value)
FROM SectorGraphs2
WHERE Kind = 1 AND Criterion = 7
AND Period = 1
AND PeriodStart >= ? AND PeriodStart < ? + INTERVAL 1 WEEK
GROUP BY SectorID


and it produces:



+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra |
+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | SectorGraphs2 | range | Producer2 | Producer2 | 6 | NULL | 1 | Using where; Using temporary; Using filesort |
+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+


See a nicely formatted explanation here.



My question: Why it is used a temporary table and filesort but it reports only 1 row examined? It seems that because of using a temporary table, it should process more than one row. How can I determine the real number of rows processed? How to solve this discrepancy about number of processed rows?



Note that the task I was assigned to do now is to eliminate heavy (involving too many rows) queries. And now I do not know how to do this.










share|improve this question
















MySQL 5.5.49-log



More questions on the query in Why does it use temporary? (MySQL) (the query is the same but the question is different):



I have the following table (filled with many rows):



CREATE TABLE `SectorGraphs2` (
`Kind` tinyint(3) UNSIGNED NOT NULL COMMENT '1 - продюсер, 2 - жанр, 3 - регион',
`Criterion` tinyint(3) UNSIGNED NOT NULL,
`Period` tinyint(3) UNSIGNED NOT NULL,
`PeriodStart` date NOT NULL,
`SectorID` int(10) UNSIGNED NOT NULL,
`Value` float NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

ALTER TABLE `SectorGraphs2`
ADD UNIQUE KEY `Producer2` (`Kind`,`Criterion`,`Period`,`PeriodStart`,`SectorID`) USING BTREE,
ADD KEY `SectorID` (`SectorID`);


then I run:



EXPLAIN 
SELECT SectorID, SUM(Value)
FROM SectorGraphs2
WHERE Kind = 1 AND Criterion = 7
AND Period = 1
AND PeriodStart >= ? AND PeriodStart < ? + INTERVAL 1 WEEK
GROUP BY SectorID


and it produces:



+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra |
+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | SectorGraphs2 | range | Producer2 | Producer2 | 6 | NULL | 1 | Using where; Using temporary; Using filesort |
+----+-------------+---------------+-------+---------------+-----------+---------+------+------+----------------------------------------------+


See a nicely formatted explanation here.



My question: Why it is used a temporary table and filesort but it reports only 1 row examined? It seems that because of using a temporary table, it should process more than one row. How can I determine the real number of rows processed? How to solve this discrepancy about number of processed rows?



Note that the task I was assigned to do now is to eliminate heavy (involving too many rows) queries. And now I do not know how to do this.







mysql performance performance-tuning explain






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 12 '17 at 10:00









MrVocabulary

1105




1105










asked Jun 5 '17 at 11:52









portonporton

3241419




3241419





bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • As Rick has explain there,"an index cannot handle both a range (PeriodStart...) and a GROUP BY" .If this is your real table and real query then I too am astonished becasue your table only contain int and date so it should be super fast.Hope you are using variable in your query as sugested below.Is estimated and actual number of rows same in plan ?Do you get same plan for other parameter also ?

    – KumarHarsh
    Jun 13 '17 at 6:40



















  • As Rick has explain there,"an index cannot handle both a range (PeriodStart...) and a GROUP BY" .If this is your real table and real query then I too am astonished becasue your table only contain int and date so it should be super fast.Hope you are using variable in your query as sugested below.Is estimated and actual number of rows same in plan ?Do you get same plan for other parameter also ?

    – KumarHarsh
    Jun 13 '17 at 6:40

















As Rick has explain there,"an index cannot handle both a range (PeriodStart...) and a GROUP BY" .If this is your real table and real query then I too am astonished becasue your table only contain int and date so it should be super fast.Hope you are using variable in your query as sugested below.Is estimated and actual number of rows same in plan ?Do you get same plan for other parameter also ?

– KumarHarsh
Jun 13 '17 at 6:40





As Rick has explain there,"an index cannot handle both a range (PeriodStart...) and a GROUP BY" .If this is your real table and real query then I too am astonished becasue your table only contain int and date so it should be super fast.Hope you are using variable in your query as sugested below.Is estimated and actual number of rows same in plan ?Do you get same plan for other parameter also ?

– KumarHarsh
Jun 13 '17 at 6:40










1 Answer
1






active

oldest

votes


















0














Try to replace AND PeriodStart >= ? AND PeriodStart < ? + INTERVAL 1 WEEK clause with precalculated version:



SET @PeriodEnd = ? + INTERVAL 1 WEEK;
SELECT SectorID, SUM(Value)
FROM SectorGraphs2
WHERE Kind = 1 AND Criterion = 7
AND Period = 1
AND PeriodStart BETWEEN ? AND @PeriodEnd
GROUP BY SectorID;


Explicitly point the index you want to use:



SET @PeriodEnd = ? + INTERVAL 1 WEEK;
SELECT SectorID, SUM(Value)
FROM SectorGraphs2 USE INDEX (`Producer2`)
WHERE Kind = 1 AND Criterion = 7
AND Period = 1
AND PeriodStart BETWEEN ? AND @PeriodEnd
GROUP BY SectorID;


Reorder fields in the index definition placing more selective fields first. To determine selectivity run the next query:



SELECT COUNT( DISTINCT Criterion )
FROM SectorGraphs2;


Then replace Criterion with Kind, PeriodStart, SectorID etc.



The higher count means better selectivity and you have to arrange fields in the index from the best selectivity to worst.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f175425%2fa-mysql-explain-number-of-rows-discrepancy%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try to replace AND PeriodStart >= ? AND PeriodStart < ? + INTERVAL 1 WEEK clause with precalculated version:



    SET @PeriodEnd = ? + INTERVAL 1 WEEK;
    SELECT SectorID, SUM(Value)
    FROM SectorGraphs2
    WHERE Kind = 1 AND Criterion = 7
    AND Period = 1
    AND PeriodStart BETWEEN ? AND @PeriodEnd
    GROUP BY SectorID;


    Explicitly point the index you want to use:



    SET @PeriodEnd = ? + INTERVAL 1 WEEK;
    SELECT SectorID, SUM(Value)
    FROM SectorGraphs2 USE INDEX (`Producer2`)
    WHERE Kind = 1 AND Criterion = 7
    AND Period = 1
    AND PeriodStart BETWEEN ? AND @PeriodEnd
    GROUP BY SectorID;


    Reorder fields in the index definition placing more selective fields first. To determine selectivity run the next query:



    SELECT COUNT( DISTINCT Criterion )
    FROM SectorGraphs2;


    Then replace Criterion with Kind, PeriodStart, SectorID etc.



    The higher count means better selectivity and you have to arrange fields in the index from the best selectivity to worst.






    share|improve this answer




























      0














      Try to replace AND PeriodStart >= ? AND PeriodStart < ? + INTERVAL 1 WEEK clause with precalculated version:



      SET @PeriodEnd = ? + INTERVAL 1 WEEK;
      SELECT SectorID, SUM(Value)
      FROM SectorGraphs2
      WHERE Kind = 1 AND Criterion = 7
      AND Period = 1
      AND PeriodStart BETWEEN ? AND @PeriodEnd
      GROUP BY SectorID;


      Explicitly point the index you want to use:



      SET @PeriodEnd = ? + INTERVAL 1 WEEK;
      SELECT SectorID, SUM(Value)
      FROM SectorGraphs2 USE INDEX (`Producer2`)
      WHERE Kind = 1 AND Criterion = 7
      AND Period = 1
      AND PeriodStart BETWEEN ? AND @PeriodEnd
      GROUP BY SectorID;


      Reorder fields in the index definition placing more selective fields first. To determine selectivity run the next query:



      SELECT COUNT( DISTINCT Criterion )
      FROM SectorGraphs2;


      Then replace Criterion with Kind, PeriodStart, SectorID etc.



      The higher count means better selectivity and you have to arrange fields in the index from the best selectivity to worst.






      share|improve this answer


























        0












        0








        0







        Try to replace AND PeriodStart >= ? AND PeriodStart < ? + INTERVAL 1 WEEK clause with precalculated version:



        SET @PeriodEnd = ? + INTERVAL 1 WEEK;
        SELECT SectorID, SUM(Value)
        FROM SectorGraphs2
        WHERE Kind = 1 AND Criterion = 7
        AND Period = 1
        AND PeriodStart BETWEEN ? AND @PeriodEnd
        GROUP BY SectorID;


        Explicitly point the index you want to use:



        SET @PeriodEnd = ? + INTERVAL 1 WEEK;
        SELECT SectorID, SUM(Value)
        FROM SectorGraphs2 USE INDEX (`Producer2`)
        WHERE Kind = 1 AND Criterion = 7
        AND Period = 1
        AND PeriodStart BETWEEN ? AND @PeriodEnd
        GROUP BY SectorID;


        Reorder fields in the index definition placing more selective fields first. To determine selectivity run the next query:



        SELECT COUNT( DISTINCT Criterion )
        FROM SectorGraphs2;


        Then replace Criterion with Kind, PeriodStart, SectorID etc.



        The higher count means better selectivity and you have to arrange fields in the index from the best selectivity to worst.






        share|improve this answer













        Try to replace AND PeriodStart >= ? AND PeriodStart < ? + INTERVAL 1 WEEK clause with precalculated version:



        SET @PeriodEnd = ? + INTERVAL 1 WEEK;
        SELECT SectorID, SUM(Value)
        FROM SectorGraphs2
        WHERE Kind = 1 AND Criterion = 7
        AND Period = 1
        AND PeriodStart BETWEEN ? AND @PeriodEnd
        GROUP BY SectorID;


        Explicitly point the index you want to use:



        SET @PeriodEnd = ? + INTERVAL 1 WEEK;
        SELECT SectorID, SUM(Value)
        FROM SectorGraphs2 USE INDEX (`Producer2`)
        WHERE Kind = 1 AND Criterion = 7
        AND Period = 1
        AND PeriodStart BETWEEN ? AND @PeriodEnd
        GROUP BY SectorID;


        Reorder fields in the index definition placing more selective fields first. To determine selectivity run the next query:



        SELECT COUNT( DISTINCT Criterion )
        FROM SectorGraphs2;


        Then replace Criterion with Kind, PeriodStart, SectorID etc.



        The higher count means better selectivity and you have to arrange fields in the index from the best selectivity to worst.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 11 '17 at 21:00









        KondybasKondybas

        2,656912




        2,656912






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f175425%2fa-mysql-explain-number-of-rows-discrepancy%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Armoriale delle famiglie italiane (Car) Indice Armi | Bibliografia | Menu di navigazioneBlasone...

            Why does this relation fail symmetry and transitivity properties?Properties of Relations. Reflexive,...

            why typing a variable (or expression) prints the value to stdout?Calling a function of a module by using its...