A MySQL EXPLAIN number of rows discrepancy
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
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.
add a comment |
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
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
add a comment |
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
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
mysql performance performance-tuning explain
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Jun 11 '17 at 21:00
KondybasKondybas
2,656912
2,656912
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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