MySQL Count where all conditions evaluate to 1
What is the best way to get count for all the records, where case evaluates to one for all the conditions. I have the query as:
SELECT
uuid ,
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '18-24' ) THEN 1 END) AS 'Profile > Age > 18-24',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '25-34' ) THEN 1 END) AS 'Profile > Age > 25-34',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Gender' and level2 = 'M' ) THEN 1 END) AS 'Profile > Gender > M',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'State' and level2 = 'QLD' ) THEN 1 END) AS 'Profile > State > QLD',
COUNT( CASE WHEN (dt = 2 and level0 = 'Insurance' and level1 = 'Health' ) THEN 1 END) AS 'Interest > Insurance > Health',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Kids' ) THEN 1 END) AS 'Profile > Kids'
FROM segmentdata sd
WHERE sd.created >= DATE_SUB(NOW(), INTERVAL 365 DAY)
GROUP BY sd.uuid;
The above query gives me a 1 and 0. I need to get a list of all one, for all the conditions and count them.
I have tried:
SELECT
COUNT(CASE WHEN ((asd.`Profile > Age > 18-24` OR asd.`Profile > Age > 25-34`)
AND (asd.`Profile > Gender > M` AND asd.`Profile > State > QLD`
AND asd.`Interest > Insurance > Health`)
AND NOT (asd.`Profile > Kids`)) THEN 1 END )
FROM (
SELECT
uuid ,
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '18-24' ) THEN 1 END) AS 'Profile > Age > 18-24',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '25-34' ) THEN 1 END) AS 'Profile > Age > 25-34',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Gender' and level2 = 'M' ) THEN 1 END) AS 'Profile > Gender > M',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'State' and level2 = 'QLD' ) THEN 1 END) AS 'Profile > State > QLD',
COUNT(CASE WHEN (dt = 2 and level0 = 'Insurance' and level1 = 'Health' ) THEN 1 END) AS 'Interest > Insurance > Health',
COUNT(CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Kids' ) THEN 1 END) AS 'Profile > Kids'
FROM segmentdata sd
WHERE sd.created >= DATE_SUB(NOW(), INTERVAL 365 DAY)
GROUP BY sd.uuid
) asd
But the above query takes a lot of time! What is the best way to do this? The above query takes about 1 -2 minutes. I need to reduce that time drastically. Please help. This is quite urgent.
My desired results are that the query runs as quickly as possible as well as gives me a count. For eg the above should give me the count from the table 'segmentdata' for All 18-34 males in QLD interested in Health insurance, without kids.
Thanks
mysql count
bumped to the homepage by Community♦ 12 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 |
What is the best way to get count for all the records, where case evaluates to one for all the conditions. I have the query as:
SELECT
uuid ,
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '18-24' ) THEN 1 END) AS 'Profile > Age > 18-24',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '25-34' ) THEN 1 END) AS 'Profile > Age > 25-34',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Gender' and level2 = 'M' ) THEN 1 END) AS 'Profile > Gender > M',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'State' and level2 = 'QLD' ) THEN 1 END) AS 'Profile > State > QLD',
COUNT( CASE WHEN (dt = 2 and level0 = 'Insurance' and level1 = 'Health' ) THEN 1 END) AS 'Interest > Insurance > Health',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Kids' ) THEN 1 END) AS 'Profile > Kids'
FROM segmentdata sd
WHERE sd.created >= DATE_SUB(NOW(), INTERVAL 365 DAY)
GROUP BY sd.uuid;
The above query gives me a 1 and 0. I need to get a list of all one, for all the conditions and count them.
I have tried:
SELECT
COUNT(CASE WHEN ((asd.`Profile > Age > 18-24` OR asd.`Profile > Age > 25-34`)
AND (asd.`Profile > Gender > M` AND asd.`Profile > State > QLD`
AND asd.`Interest > Insurance > Health`)
AND NOT (asd.`Profile > Kids`)) THEN 1 END )
FROM (
SELECT
uuid ,
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '18-24' ) THEN 1 END) AS 'Profile > Age > 18-24',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '25-34' ) THEN 1 END) AS 'Profile > Age > 25-34',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Gender' and level2 = 'M' ) THEN 1 END) AS 'Profile > Gender > M',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'State' and level2 = 'QLD' ) THEN 1 END) AS 'Profile > State > QLD',
COUNT(CASE WHEN (dt = 2 and level0 = 'Insurance' and level1 = 'Health' ) THEN 1 END) AS 'Interest > Insurance > Health',
COUNT(CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Kids' ) THEN 1 END) AS 'Profile > Kids'
FROM segmentdata sd
WHERE sd.created >= DATE_SUB(NOW(), INTERVAL 365 DAY)
GROUP BY sd.uuid
) asd
But the above query takes a lot of time! What is the best way to do this? The above query takes about 1 -2 minutes. I need to reduce that time drastically. Please help. This is quite urgent.
My desired results are that the query runs as quickly as possible as well as gives me a count. For eg the above should give me the count from the table 'segmentdata' for All 18-34 males in QLD interested in Health insurance, without kids.
Thanks
mysql count
bumped to the homepage by Community♦ 12 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
how much data is this running on? Also, are there any indexes on the table?
– DForck42
Jan 26 '15 at 22:35
There are about 2 million and over records and the indices on the table are like: <code> KEYHASHINDEX(hash), KEYUUIDINDEX(uuid), KEYCREATEDINDEX(created), KEYUUID_CREATEDINDEX(uuid,created) </code>
– Newbie
Jan 26 '15 at 22:57
add a comment |
What is the best way to get count for all the records, where case evaluates to one for all the conditions. I have the query as:
SELECT
uuid ,
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '18-24' ) THEN 1 END) AS 'Profile > Age > 18-24',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '25-34' ) THEN 1 END) AS 'Profile > Age > 25-34',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Gender' and level2 = 'M' ) THEN 1 END) AS 'Profile > Gender > M',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'State' and level2 = 'QLD' ) THEN 1 END) AS 'Profile > State > QLD',
COUNT( CASE WHEN (dt = 2 and level0 = 'Insurance' and level1 = 'Health' ) THEN 1 END) AS 'Interest > Insurance > Health',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Kids' ) THEN 1 END) AS 'Profile > Kids'
FROM segmentdata sd
WHERE sd.created >= DATE_SUB(NOW(), INTERVAL 365 DAY)
GROUP BY sd.uuid;
The above query gives me a 1 and 0. I need to get a list of all one, for all the conditions and count them.
I have tried:
SELECT
COUNT(CASE WHEN ((asd.`Profile > Age > 18-24` OR asd.`Profile > Age > 25-34`)
AND (asd.`Profile > Gender > M` AND asd.`Profile > State > QLD`
AND asd.`Interest > Insurance > Health`)
AND NOT (asd.`Profile > Kids`)) THEN 1 END )
FROM (
SELECT
uuid ,
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '18-24' ) THEN 1 END) AS 'Profile > Age > 18-24',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '25-34' ) THEN 1 END) AS 'Profile > Age > 25-34',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Gender' and level2 = 'M' ) THEN 1 END) AS 'Profile > Gender > M',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'State' and level2 = 'QLD' ) THEN 1 END) AS 'Profile > State > QLD',
COUNT(CASE WHEN (dt = 2 and level0 = 'Insurance' and level1 = 'Health' ) THEN 1 END) AS 'Interest > Insurance > Health',
COUNT(CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Kids' ) THEN 1 END) AS 'Profile > Kids'
FROM segmentdata sd
WHERE sd.created >= DATE_SUB(NOW(), INTERVAL 365 DAY)
GROUP BY sd.uuid
) asd
But the above query takes a lot of time! What is the best way to do this? The above query takes about 1 -2 minutes. I need to reduce that time drastically. Please help. This is quite urgent.
My desired results are that the query runs as quickly as possible as well as gives me a count. For eg the above should give me the count from the table 'segmentdata' for All 18-34 males in QLD interested in Health insurance, without kids.
Thanks
mysql count
What is the best way to get count for all the records, where case evaluates to one for all the conditions. I have the query as:
SELECT
uuid ,
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '18-24' ) THEN 1 END) AS 'Profile > Age > 18-24',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '25-34' ) THEN 1 END) AS 'Profile > Age > 25-34',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Gender' and level2 = 'M' ) THEN 1 END) AS 'Profile > Gender > M',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'State' and level2 = 'QLD' ) THEN 1 END) AS 'Profile > State > QLD',
COUNT( CASE WHEN (dt = 2 and level0 = 'Insurance' and level1 = 'Health' ) THEN 1 END) AS 'Interest > Insurance > Health',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Kids' ) THEN 1 END) AS 'Profile > Kids'
FROM segmentdata sd
WHERE sd.created >= DATE_SUB(NOW(), INTERVAL 365 DAY)
GROUP BY sd.uuid;
The above query gives me a 1 and 0. I need to get a list of all one, for all the conditions and count them.
I have tried:
SELECT
COUNT(CASE WHEN ((asd.`Profile > Age > 18-24` OR asd.`Profile > Age > 25-34`)
AND (asd.`Profile > Gender > M` AND asd.`Profile > State > QLD`
AND asd.`Interest > Insurance > Health`)
AND NOT (asd.`Profile > Kids`)) THEN 1 END )
FROM (
SELECT
uuid ,
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '18-24' ) THEN 1 END) AS 'Profile > Age > 18-24',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Age_Group' and level2 = '25-34' ) THEN 1 END) AS 'Profile > Age > 25-34',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Gender' and level2 = 'M' ) THEN 1 END) AS 'Profile > Gender > M',
COUNT( CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'State' and level2 = 'QLD' ) THEN 1 END) AS 'Profile > State > QLD',
COUNT(CASE WHEN (dt = 2 and level0 = 'Insurance' and level1 = 'Health' ) THEN 1 END) AS 'Interest > Insurance > Health',
COUNT(CASE WHEN (( dt = 1 or level0 = 'Profile' ) and level1 = 'Kids' ) THEN 1 END) AS 'Profile > Kids'
FROM segmentdata sd
WHERE sd.created >= DATE_SUB(NOW(), INTERVAL 365 DAY)
GROUP BY sd.uuid
) asd
But the above query takes a lot of time! What is the best way to do this? The above query takes about 1 -2 minutes. I need to reduce that time drastically. Please help. This is quite urgent.
My desired results are that the query runs as quickly as possible as well as gives me a count. For eg the above should give me the count from the table 'segmentdata' for All 18-34 males in QLD interested in Health insurance, without kids.
Thanks
mysql count
mysql count
edited Jan 27 '15 at 21:54
DForck42
1,73632155
1,73632155
asked Jan 26 '15 at 21:17
NewbieNewbie
61
61
bumped to the homepage by Community♦ 12 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♦ 12 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
how much data is this running on? Also, are there any indexes on the table?
– DForck42
Jan 26 '15 at 22:35
There are about 2 million and over records and the indices on the table are like: <code> KEYHASHINDEX(hash), KEYUUIDINDEX(uuid), KEYCREATEDINDEX(created), KEYUUID_CREATEDINDEX(uuid,created) </code>
– Newbie
Jan 26 '15 at 22:57
add a comment |
how much data is this running on? Also, are there any indexes on the table?
– DForck42
Jan 26 '15 at 22:35
There are about 2 million and over records and the indices on the table are like: <code> KEYHASHINDEX(hash), KEYUUIDINDEX(uuid), KEYCREATEDINDEX(created), KEYUUID_CREATEDINDEX(uuid,created) </code>
– Newbie
Jan 26 '15 at 22:57
how much data is this running on? Also, are there any indexes on the table?
– DForck42
Jan 26 '15 at 22:35
how much data is this running on? Also, are there any indexes on the table?
– DForck42
Jan 26 '15 at 22:35
There are about 2 million and over records and the indices on the table are like: <code> KEY
HASHINDEX (hash), KEY UUIDINDEX (uuid), KEY CREATEDINDEX (created), KEY UUID_CREATEDINDEX (uuid,created) </code>– Newbie
Jan 26 '15 at 22:57
There are about 2 million and over records and the indices on the table are like: <code> KEY
HASHINDEX (hash), KEY UUIDINDEX (uuid), KEY CREATEDINDEX (created), KEY UUID_CREATEDINDEX (uuid,created) </code>– Newbie
Jan 26 '15 at 22:57
add a comment |
2 Answers
2
active
oldest
votes
I'm just guessing at your data structure from your query, but it looks like it'd be a lot easier to use derived tables for your problem. Here's what it should roughly look like:
select
count(distinct age.uuid)
from
(
select uuid
from segmentdata
where ( dt = 1 or level0 = 'Profile' )
and level1 = 'Age_Group'
and level2 in ('18-24', '25-34')
) age
inner join
(
select uuid
from segmentdata
where ( dt = 1 or level0 = 'Profile' )
and level1 = 'Gender'
and level2 = 'M'
) gender
on age.uuid = gender.uuid
This queries creates several derived tables based on what you want to filter on, and then joins all of the uuid's together. In effect, you should only get uuid's that are in all of the derived tables.
If this doesn't perform fast enough, then try creating an index on: uuid, dt, level0, level1, level2, and created
sqlfiddle.com/#!2/8b5aaaf/3
– Newbie
Jan 29 '15 at 1:30
@user57391 I don't knwo what you want me to see
– DForck42
Jan 29 '15 at 5:47
The SQLFiddle has a few data that I want to do a select count on. I am trying to see if there is a better way, this can be done like, or if there is any other way!
– Newbie
Jan 29 '15 at 20:54
add a comment |
Use SUM() instead of COUNT() when the expression evaluates to 0 or 1. That is...
COUNT(CASE WHEN condition THEN 1 END)
-->
SUM(condition)
This should help performance:
INDEX(created, uuid)
Perhaps you don't have a big enough cache? SHOW VARIABLES LIKE '%buffer%'; and how much RAM do you have?
Please show us SHOW CREATE TABLE segmentdata for further discussion.
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%2f90236%2fmysql-count-where-all-conditions-evaluate-to-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm just guessing at your data structure from your query, but it looks like it'd be a lot easier to use derived tables for your problem. Here's what it should roughly look like:
select
count(distinct age.uuid)
from
(
select uuid
from segmentdata
where ( dt = 1 or level0 = 'Profile' )
and level1 = 'Age_Group'
and level2 in ('18-24', '25-34')
) age
inner join
(
select uuid
from segmentdata
where ( dt = 1 or level0 = 'Profile' )
and level1 = 'Gender'
and level2 = 'M'
) gender
on age.uuid = gender.uuid
This queries creates several derived tables based on what you want to filter on, and then joins all of the uuid's together. In effect, you should only get uuid's that are in all of the derived tables.
If this doesn't perform fast enough, then try creating an index on: uuid, dt, level0, level1, level2, and created
sqlfiddle.com/#!2/8b5aaaf/3
– Newbie
Jan 29 '15 at 1:30
@user57391 I don't knwo what you want me to see
– DForck42
Jan 29 '15 at 5:47
The SQLFiddle has a few data that I want to do a select count on. I am trying to see if there is a better way, this can be done like, or if there is any other way!
– Newbie
Jan 29 '15 at 20:54
add a comment |
I'm just guessing at your data structure from your query, but it looks like it'd be a lot easier to use derived tables for your problem. Here's what it should roughly look like:
select
count(distinct age.uuid)
from
(
select uuid
from segmentdata
where ( dt = 1 or level0 = 'Profile' )
and level1 = 'Age_Group'
and level2 in ('18-24', '25-34')
) age
inner join
(
select uuid
from segmentdata
where ( dt = 1 or level0 = 'Profile' )
and level1 = 'Gender'
and level2 = 'M'
) gender
on age.uuid = gender.uuid
This queries creates several derived tables based on what you want to filter on, and then joins all of the uuid's together. In effect, you should only get uuid's that are in all of the derived tables.
If this doesn't perform fast enough, then try creating an index on: uuid, dt, level0, level1, level2, and created
sqlfiddle.com/#!2/8b5aaaf/3
– Newbie
Jan 29 '15 at 1:30
@user57391 I don't knwo what you want me to see
– DForck42
Jan 29 '15 at 5:47
The SQLFiddle has a few data that I want to do a select count on. I am trying to see if there is a better way, this can be done like, or if there is any other way!
– Newbie
Jan 29 '15 at 20:54
add a comment |
I'm just guessing at your data structure from your query, but it looks like it'd be a lot easier to use derived tables for your problem. Here's what it should roughly look like:
select
count(distinct age.uuid)
from
(
select uuid
from segmentdata
where ( dt = 1 or level0 = 'Profile' )
and level1 = 'Age_Group'
and level2 in ('18-24', '25-34')
) age
inner join
(
select uuid
from segmentdata
where ( dt = 1 or level0 = 'Profile' )
and level1 = 'Gender'
and level2 = 'M'
) gender
on age.uuid = gender.uuid
This queries creates several derived tables based on what you want to filter on, and then joins all of the uuid's together. In effect, you should only get uuid's that are in all of the derived tables.
If this doesn't perform fast enough, then try creating an index on: uuid, dt, level0, level1, level2, and created
I'm just guessing at your data structure from your query, but it looks like it'd be a lot easier to use derived tables for your problem. Here's what it should roughly look like:
select
count(distinct age.uuid)
from
(
select uuid
from segmentdata
where ( dt = 1 or level0 = 'Profile' )
and level1 = 'Age_Group'
and level2 in ('18-24', '25-34')
) age
inner join
(
select uuid
from segmentdata
where ( dt = 1 or level0 = 'Profile' )
and level1 = 'Gender'
and level2 = 'M'
) gender
on age.uuid = gender.uuid
This queries creates several derived tables based on what you want to filter on, and then joins all of the uuid's together. In effect, you should only get uuid's that are in all of the derived tables.
If this doesn't perform fast enough, then try creating an index on: uuid, dt, level0, level1, level2, and created
answered Jan 27 '15 at 21:12
DForck42DForck42
1,73632155
1,73632155
sqlfiddle.com/#!2/8b5aaaf/3
– Newbie
Jan 29 '15 at 1:30
@user57391 I don't knwo what you want me to see
– DForck42
Jan 29 '15 at 5:47
The SQLFiddle has a few data that I want to do a select count on. I am trying to see if there is a better way, this can be done like, or if there is any other way!
– Newbie
Jan 29 '15 at 20:54
add a comment |
sqlfiddle.com/#!2/8b5aaaf/3
– Newbie
Jan 29 '15 at 1:30
@user57391 I don't knwo what you want me to see
– DForck42
Jan 29 '15 at 5:47
The SQLFiddle has a few data that I want to do a select count on. I am trying to see if there is a better way, this can be done like, or if there is any other way!
– Newbie
Jan 29 '15 at 20:54
sqlfiddle.com/#!2/8b5aaaf/3
– Newbie
Jan 29 '15 at 1:30
sqlfiddle.com/#!2/8b5aaaf/3
– Newbie
Jan 29 '15 at 1:30
@user57391 I don't knwo what you want me to see
– DForck42
Jan 29 '15 at 5:47
@user57391 I don't knwo what you want me to see
– DForck42
Jan 29 '15 at 5:47
The SQLFiddle has a few data that I want to do a select count on. I am trying to see if there is a better way, this can be done like, or if there is any other way!
– Newbie
Jan 29 '15 at 20:54
The SQLFiddle has a few data that I want to do a select count on. I am trying to see if there is a better way, this can be done like, or if there is any other way!
– Newbie
Jan 29 '15 at 20:54
add a comment |
Use SUM() instead of COUNT() when the expression evaluates to 0 or 1. That is...
COUNT(CASE WHEN condition THEN 1 END)
-->
SUM(condition)
This should help performance:
INDEX(created, uuid)
Perhaps you don't have a big enough cache? SHOW VARIABLES LIKE '%buffer%'; and how much RAM do you have?
Please show us SHOW CREATE TABLE segmentdata for further discussion.
add a comment |
Use SUM() instead of COUNT() when the expression evaluates to 0 or 1. That is...
COUNT(CASE WHEN condition THEN 1 END)
-->
SUM(condition)
This should help performance:
INDEX(created, uuid)
Perhaps you don't have a big enough cache? SHOW VARIABLES LIKE '%buffer%'; and how much RAM do you have?
Please show us SHOW CREATE TABLE segmentdata for further discussion.
add a comment |
Use SUM() instead of COUNT() when the expression evaluates to 0 or 1. That is...
COUNT(CASE WHEN condition THEN 1 END)
-->
SUM(condition)
This should help performance:
INDEX(created, uuid)
Perhaps you don't have a big enough cache? SHOW VARIABLES LIKE '%buffer%'; and how much RAM do you have?
Please show us SHOW CREATE TABLE segmentdata for further discussion.
Use SUM() instead of COUNT() when the expression evaluates to 0 or 1. That is...
COUNT(CASE WHEN condition THEN 1 END)
-->
SUM(condition)
This should help performance:
INDEX(created, uuid)
Perhaps you don't have a big enough cache? SHOW VARIABLES LIKE '%buffer%'; and how much RAM do you have?
Please show us SHOW CREATE TABLE segmentdata for further discussion.
answered Feb 8 '15 at 20:02
Rick JamesRick James
43.3k22259
43.3k22259
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%2f90236%2fmysql-count-where-all-conditions-evaluate-to-1%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
how much data is this running on? Also, are there any indexes on the table?
– DForck42
Jan 26 '15 at 22:35
There are about 2 million and over records and the indices on the table are like: <code> KEY
HASHINDEX(hash), KEYUUIDINDEX(uuid), KEYCREATEDINDEX(created), KEYUUID_CREATEDINDEX(uuid,created) </code>– Newbie
Jan 26 '15 at 22:57