query taking too much time with irritated performance [closed] - mysql

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
My query is taking very long time around 1hr
it was impacting on DB performance.
even for Explain plan also it was taking very long time.
please rewrite the query for optimal performance.
Query:
SELECT Count(*)
FROM (SELECT paid.keyword_id,
paid.keyword_name,
stat.orgentrances,
keyword.rank1,
keyword.rank_check,
Sum(paid.clicks) AS sumclick,
Sum(paid.clicks * paid.avg_position) AS
sumclickavgpos,
Sum(paid.itemrevenue) AS sumitem,
Sum(paid.cost) AS sumcost,
Sum(paid.transactions) AS sumtrans,
Sum(paid.impressions) AS
sumimpress,
IF(Sum(paid.impressions) = 0, 0, Sum(
paid.impressions * paid.avg_position) / Sum
(paid.impressions)) AS
sumimpressavgrank,
con.item_revenue,
con.transactions,
keyword.monthly_search_volume
FROM `t_keyword_paid_analytics_google_ib` paid
LEFT JOIN (SELECT outer_t.keyword_id,
Sum(outer_t.item_revenue) AS item_revenue,
Sum(outer_t.transactions) AS transactions
FROM t_keyword_conversion_ga_ib outer_t
WHERE outer_t.own_domain_id = 720
AND outer_t.traffic_date >= '2012-12-01'
AND outer_t.traffic_date <= '2012-12-31'
GROUP BY outer_t.keyword_id) con
ON paid.keyword_id = con.keyword_id
LEFT JOIN (SELECT outer_t.keyword_id,
Sum(outer_t.entrances) AS orgEntrances
FROM t_keyword_stat_ga_ib outer_t
WHERE outer_t.own_domain_id = 720
AND outer_t.traffic_date >= '2012-12-01'
AND outer_t.traffic_date <= '2012-12-31'
AND ( outer_t.medium = 'organic'
OR outer_t.medium IS NULL )
GROUP BY outer_t.keyword_id) stat
ON paid.keyword_id = stat.keyword_id
LEFT JOIN `t_managed_keyword_ib` keyword
ON keyword.id = paid.keyword_id
WHERE paid.own_domain_id = 720
AND paid.traffic_date >= '2012-12-01'
AND paid.traffic_date <= '2012-12-31'
AND ( paid.channel IS NULL
OR paid.channel = 'Google' )
GROUP BY paid.keyword_id
HAVING paid.keyword_id IS NOT NULL) tempt;
(outer_t.medium = 'organic' or outer_t.medium is null ) after outer_t.own_domain_id = 720 and (paid.channel is null or paid.channel = 'Google')
after paid.own_domain_id = 720
Tables Structure:
mysql> show create table t_keyword_paid_analytics_google_ib\G
*************************** 1. row ***************************
Table: t_keyword_paid_analytics_google_ib
Create Table: CREATE TABLE `t_keyword_paid_analytics_google_ib` (
`keyword_name` varchar(255) DEFAULT NULL,
`id` int(11) NOT NULL,
`keyword_id` int(11) NOT NULL,
`target_url_id` int(11) DEFAULT NULL,
`own_domain_id` int(11) NOT NULL,
`log_date` date NOT NULL,
`traffic_date` date NOT NULL,
`impressions` int(11) DEFAULT NULL,
`clicks` int(11) DEFAULT NULL,
`entrances` int(11) DEFAULT NULL,
`match_type` int(11) DEFAULT NULL COMMENT '1: Phrase, 2: Exact, 3:Broad 4: etc',
`ad_group_name` varchar(200) DEFAULT NULL,
`ad_distribution_network` varchar(500) CHARACTER SET latin1 DEFAULT NULL,
`match_query` varchar(500) CHARACTER SET latin1 DEFAULT NULL,
`cost` decimal(10,2) DEFAULT NULL,
`cpm` decimal(10,2) DEFAULT NULL,
`ctr` decimal(10,2) DEFAULT NULL COMMENT 'percent',
`cpc` decimal(10,2) DEFAULT NULL,
`campaign` varchar(200) CHARACTER SET latin1 DEFAULT NULL,
`keyword_status` tinyint(4) DEFAULT NULL COMMENT '1: Active,2: Approved, 3: Disapproved, 4: Paused , 5:Pending, 6: Failed, 7:etc',
`ad_group_status` tinyint(4) DEFAULT NULL COMMENT '1:ELIGIBLE; 2=PAUSED;3=LOW_SEARCH_VOLUME;4 =LOW_QUALITY_SCORE; 5=DISAPPROVED; 6=AD_GROUP_PAUSED; 7=etc',
`max_cpc` decimal(10,2) DEFAULT NULL,
`quality_score` tinyint(4) DEFAULT NULL,
`channel` varchar(100) DEFAULT NULL,
`first_page_cpc` decimal(10,2) DEFAULT NULL,
`avg_position` decimal(10,2) DEFAULT NULL,
`itemRevenue` decimal(10,2) DEFAULT NULL,
`goal1value` decimal(10,2) DEFAULT NULL,
`goal2value` decimal(10,2) DEFAULT NULL,
`goal3value` decimal(10,2) DEFAULT NULL,
`goal4value` decimal(10,2) DEFAULT NULL,
`transactions` int(10) DEFAULT NULL,
`goal1completions` int(10) DEFAULT NULL,
`goal2completions` int(10) DEFAULT NULL,
`goal3completions` int(10) DEFAULT NULL,
`goal4completions` int(10) DEFAULT NULL
) ENGINE=BRIGHTHOUSE DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> show create table t_keyword_conversion_ga_ib\G
*************************** 1. row ***************************
Table: t_keyword_conversion_ga_ib
Create Table: CREATE TABLE `t_keyword_conversion_ga_ib` (
`keyword_name` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`id` int(11) NOT NULL,
`own_domain_id` int(11) DEFAULT NULL,
`keyword_id` int(11) DEFAULT NULL,
`traffic_date` date DEFAULT NULL,
`targeturl_id` int(11) DEFAULT NULL,
`entrance` int(11) DEFAULT NULL,
`transactions` int(11) DEFAULT NULL,
`item_revenue` decimal(9,2) DEFAULT NULL,
`goal1completions` int(11) DEFAULT NULL,
`goal2completions` int(11) DEFAULT NULL,
`goal3completions` int(11) DEFAULT NULL,
`goal4completions` int(11) DEFAULT NULL,
`goal5completions` int(11) DEFAULT NULL,
`goal6completions` int(11) DEFAULT NULL,
`goal7completions` int(11) DEFAULT NULL,
`goal8completions` int(11) DEFAULT NULL,
`goal9completions` int(11) DEFAULT NULL,
`goal10completions` int(11) DEFAULT NULL,
`goal1Value` decimal(9,2) DEFAULT NULL,
`goal2Value` decimal(9,2) DEFAULT NULL,
`goal3Value` decimal(9,2) DEFAULT NULL,
`goal4Value` decimal(9,2) DEFAULT NULL,
`goal5Value` decimal(9,2) DEFAULT NULL,
`goal6Value` decimal(9,2) DEFAULT NULL,
`goal7Value` decimal(9,2) DEFAULT NULL,
`goal8Value` decimal(9,2) DEFAULT NULL,
`goal9Value` decimal(9,2) DEFAULT NULL,
`goal10Value` decimal(9,2) DEFAULT NULL,
`medium` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`source` varchar(255) COLLATE latin1_bin DEFAULT NULL
) ENGINE=BRIGHTHOUSE DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.00 sec)
please help me here.

You don't indicate any indexes. You will need them to improve performance.
OK, I don't understand the part after the semi-colon, so I'll ignore that.
Your whole query does a count(*) of a sub select, but you are doing a bunch of aggregation in the sub-query (calling SUM) which is work the database doesn't have to do, so eliminate that.
You do aggregation in further sub-queries, so cut that out.
In your main sub-select, where you are just doing a count(*), you select fields that are not in your group by, which is work on the database that is not affecting the count, so get rid of that.
That gives us something like...
select count(*)
from (select paid.keyword_id
from `t_keyword_paid_analytics_google_ib` paid
left join
(select outer_t.keyword_id
from t_keyword_conversion_ga_ib outer_t
where ...
group by outer_t.keyword_id ) con
on paid.keyword_id = con.keyword_id
left join
(select outer_t.keyword_id
from t_keyword_stat_ga_ib outer_t
where ...
group by outer_t.keyword_id) stat on paid.keyword_id = stat.keyword_id
left join `t_managed_keyword_ib` keyword on keyword.id = paid.keyword_id
where ...
group by paid.keyword_id
having paid.keyword_id is not null
) tempt;
Also, counts may be faster than selects and you seem to be looking for the keyword_id and counting them in the end. If you can count as you go that would be better than selecting. I'm not sure if you can apply that to this query, but keep it in mind.

You have to add some index to your tables,
ALTER TABLE table_name ADD UNIQUE INDEX cate_id(field_name);
here is the documentation
In your case I would add index to:
paid.keyword_id
con.keyword_id
stat.keyword_id
keyword.id
paid.keyword_id
because you are making join on those fields

Related

error in my mysql query with a limit condition

i have 3 tables
customers, times and sales
i want to find out all the customers income yearly condition is that customers with no children and income must be greater than a limit we are set
my table structure
customers
CREATE TABLE `customers` (
`customer_id` int(11) DEFAULT NULL,
`account_num` double DEFAULT NULL,
`lname` varchar(50) DEFAULT NULL,
`fname` varchar(50) DEFAULT NULL,
`mi` varchar(50) DEFAULT NULL,
`address1` varchar(50) DEFAULT NULL,
`address2` varchar(50) DEFAULT NULL,
`address3` varchar(50) DEFAULT NULL,
`address4` varchar(50) DEFAULT NULL,
`postal_code` varchar(50) DEFAULT NULL,
`region_id` int(11) DEFAULT NULL,
`phone1` varchar(50) DEFAULT NULL,
`phone2` varchar(50) DEFAULT NULL,
`birthdate` datetime DEFAULT NULL,
`marital_status` varchar(50) DEFAULT NULL,
`yearly_income` varchar(50) DEFAULT NULL,
`gender` varchar(50) DEFAULT NULL,
`total_children` smallint(6) DEFAULT NULL,
`num_children_at_home` smallint(6) DEFAULT NULL,
`education` varchar(50) DEFAULT NULL,
`member_card` varchar(50) DEFAULT NULL,
`occupation` varchar(50) DEFAULT NULL,
`houseowner` varchar(50) DEFAULT NULL,
`num_cars_owned` smallint(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
sales
CREATE TABLE `sales` (
`product_id` int(11) DEFAULT NULL,
`time_id` int(11) DEFAULT NULL,
`customer_id` int(11) DEFAULT NULL,
`store_id` int(11) DEFAULT NULL,
`store_sales` float DEFAULT NULL,
`store_cost` float DEFAULT NULL,
`unit_sales` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
times
CREATE TABLE `times` (
`time_id` int(11) DEFAULT NULL,
`the_date` datetime DEFAULT NULL,
`the_day` varchar(50) DEFAULT NULL,
`the_month` varchar(50) DEFAULT NULL,
`the_year` smallint(6) DEFAULT NULL,
`day_of_month` smallint(6) DEFAULT NULL,
`month_of_year` smallint(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
MY question is :-Find the list of the customers with no child and an yearly_income greater that a limit given by the user when running the query.
MY query is
SET #limit=50;
SELECT customers.`fname`, customers.`lname` ,ROUND(SUM(sales.store_sales)) as income,times.the_year
FROM `sales`
LEFT JOIN times
ON sales.time_id=times.time_id
LEFT JOIN customers
ON customers.customer_id=sales.customer_id
WHERE income>#limit AND `total_children`=0
GROUP BY sales.customer_id,times.the_year
am getting this error:#1054 - Unknown column 'income' in 'where clause'
The quantity you aliased as income is an aggregate, and therefore it does not make sense to refer to it in the WHERE clause. Move this WHERE logic to a HAVING clause:
SET #limit=50;
SELECT
c.fname,
c.lname,
ROUND(SUM(s.store_sales)) AS income,
t.the_year
FROM sales s
LEFT JOIN times t
ON s.time_id = t.time_id
LEFT JOIN customers c
ON c.customer_id = s.customer_id
WHERE
total_children = 0
GROUP BY
c.customer_id,
t.the_year
HAVING
ROUND(SUM(s.store_sales)) > #limit;
Note that technically we could have used the alias in the HAVING clause:
HAVING income > #limit;
But this would not be portable to most other databases. Also, I introduced aliases into the query, which make it easier to read.
For aggreagte columns not present in the original table you should use having clause instead of where
SET #limit=50;
SELECT customers.`fname`, customers.`lname` ,ROUND(SUM(sales.store_sales)) as income,times.the_year
FROM `sales`
LEFT JOIN times
ON sales.time_id=times.time_id
LEFT JOIN customers
ON customers.customer_id=sales.customer_id
WHERE `total_children`=0
GROUP BY customers.customer_id,times.the_year
having income>#limit
income is alias name,so can't use that in where condition.
SET #limit=50;
SELECT * FROM
(
SELECT customers.`fname`, customers.`lname` ,ROUND(SUM(sales.store_sales))
as income,times.the_year
FROM `sales` LEFT JOIN times ON sales.time_id=times.time_id
LEFT JOIN customers ON customers.customer_id=sales.customer_id
WHERE `total_children`=0
GROUP BY sales.customer_id,times.the_year
)t WHERE income>#limit
Find the list of the customers with no child and an yearly_income
greater that a limit given by the user when running the query.
Are you sure you don't want a simple query like this?
SELECT *
FROM customers AS c
WHERE yearly_income > #limit -- but why is yearly_income a VarChar(50)?
AND total_children=0

How can I select unanswered questions?

I have a table like this:
CREATE TABLE `qanda` (
`id` int(11) UNSIGNED NOT NULL,
`subject` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`body` text COLLATE utf8_unicode_ci NOT NULL,
`body_html` text COLLATE utf8_unicode_ci NOT NULL,
`related` int(11) UNSIGNED DEFAULT NULL,
`type` tinyint(1) NOT NULL,
`amount` decimal(11,0) DEFAULT NULL,
`closed` tinyint(1) UNSIGNED DEFAULT NULL,
`CloserId` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`AcceptedAnswer` tinyint(1) DEFAULT NULL,
`aadate` int(11) UNSIGNED DEFAULT NULL,
`category` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`keywords` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`visibility` tinyint(1) NOT NULL,
`author_id` int(11) UNSIGNED DEFAULT NULL,
`editor_id` int(11) UNSIGNED DEFAULT NULL,
`date_time` int(11) UNSIGNED NOT NULL,
`edited_at` int(11) UNSIGNED DEFAULT NULL,
`activated_at` int(11) UNSIGNED DEFAULT NULL,
`activated_story` enum('سوال شده','ویرایش شده','جواب داده شده','') COLLATE utf8_unicode_ci NOT NULL,
`activator_id` int(11) UNSIGNED DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
This table stores both questions and answers. Here is my current query which provides a list of all questions and sort them based on the vote:
SELECT *, (SELECT COALESCE(sum(vv.value),0)
FROM votes vv
WHERE qanda.id = vv.post_id) AS total_votes
FROM qanda
WHERE type = 0 -- "type=0" means questions
ORDER BY total_votes DESC
LIMIT :j,11;
Now I need one more condition on the WHERE clause to exclude questions that have answer(s). I guess I need a self-join. But I don't know how can I write a join on the where clause. Is there any suggestion?
Use NOT IN to exclude IDs that were answered:
SELECT *, (SELECT COALESCE(sum(vv.value),0)
FROM votes vv
WHERE qanda.id = vv.post_id) AS total_votes
FROM qanda
WHERE type = 0
AND id NOT IN (SELECT related FROM quanda WHERE type <> 0)
ORDER BY total_votes DESC
LIMIT :j,11;
With a SELF JOIN, you try to match a question with any answer. If you don't find a match you will get NULL so you will get the questions without answers.
SELECT questions.*
FROM qanda questions
LEFT JOIN qanda answers
ON question.id = answers.related
WHERE questions.type = 0
AND answers.id IS NULL

Mysql select * from a where doesnt exist in b

Im having problems with constructing a query which filters out posts from table a, that already existing in table b.
Table A:
`bok_id` int(6) NOT NULL AUTO_INCREMENT,
`tid` time NOT NULL,
`datum` date NOT NULL,
`datum_end` date NOT NULL,
`framtid` varchar(5) NOT NULL,
`h_adress` varchar(100) NOT NULL,
`l_adress` varchar(100) NOT NULL,
`kund` varchar(100) NOT NULL,
`typ` varchar(5) NOT NULL,
`bil` varchar(99) NOT NULL,
`sign` varchar(2) NOT NULL,
`tilldelad` varchar(12) NOT NULL,
`skapad` datetime NOT NULL,
`endrad` datetime NOT NULL,
`endrad_av` varchar(2) NOT NULL,
`kommentar` text NOT NULL,
`weektype` varchar(3) NOT NULL,
`Monday` tinyint(1) NOT NULL,
`Tuesday` tinyint(1) NOT NULL,
`Wednesday` tinyint(1) NOT NULL,
`Thursday` tinyint(1) NOT NULL,
`Friday` tinyint(1) NOT NULL,
`Saturday` tinyint(1) NOT NULL,
`Sunday` tinyint(1) NOT NULL,
`avbokad` varchar(2) NOT NULL,
`unika_kommentarer` varchar(2) NOT NULL,
UNIQUE KEY `bok_id` (`bok_id`)
Table B:
`id` int(6) NOT NULL AUTO_INCREMENT,
`bok_id` int(6) NOT NULL,
`tid` time NOT NULL,
`datum` date NOT NULL,
`datum_end` date NOT NULL,
`framtid` varchar(5) NOT NULL,
`h_adress` varchar(100) NOT NULL,
`l_adress` varchar(100) NOT NULL,
`kund` varchar(100) NOT NULL,
`typ` varchar(5) NOT NULL,
`bil` varchar(99) NOT NULL,
`sign` varchar(2) NOT NULL,
`tilldelad` varchar(12) NOT NULL,
`skapad` datetime NOT NULL,
`endrad` datetime NOT NULL,
`endrad_av` varchar(2) NOT NULL,
`kommentar` text NOT NULL,
`weektype` varchar(3) NOT NULL,
`Monday` tinyint(1) NOT NULL,
`Tuesday` tinyint(1) NOT NULL,
`Wednesday` tinyint(1) NOT NULL,
`Thursday` tinyint(1) NOT NULL,
`Friday` tinyint(1) NOT NULL,
`Saturday` tinyint(1) NOT NULL,
`Sunday` tinyint(1) NOT NULL,
`avbokad` varchar(2) NOT NULL,
`unika_kommentarer` varchar(2) NOT NULL,
UNIQUE KEY `id` (`id`)
What i want is a query which hides all rows in Table A which exists in table B IF Table B.tilldelad=Requested Date. (f.e 2013-09-30)
Im not sure this makes any sense?
What i want is to filter out period bookings as they have been executed and therefore exists in table b which indicates it has been.
Since its recurring events the same bok_id can exists SEVERAL times in table B, but only ONCE in table A...
SELECT * FROM bokningar
WHERE bokningar.datum <= '2013-09-30'
AND bokningar.datum_end >= '2013-09-30'
AND bokningar.typ >= '2'
AND bokningar.weektype = '1'
AND bokningar.Monday = '1' ## Monday this is dynamically changed to current date
AND bokningar.avbokad < '1'
AND NOT EXISTS ( SELECT 1 FROM tilldelade WHERE tilldelade.tilldelad = '2013-09-30' )
The above code does the trick regarding filtering out rows not in table B, however, if one row in table B has the current date, all results are filtered out.
Only rows with corresponding bok_id's is supposted to get filtered out.
Any thoughts on how to do that? Perhaps a Distinct select?
Typically, NOT IN subselects are quite expensive in querying time. This can normally be handled by doing a LEFT-JOIN and Keeping only those where the other table IS NULL like...
SELECT *
FROM
bokningar
LEFT JOIN tilldelade
on bokningar.bok_id = tilldelade.bok_id
AND tilldelade.tilldelad = '2013-09-30'
WHERE
bokningar.datum <= '2013-09-30'
AND bokningar.datum_end >= '2013-09-30'
AND bokningar.typ >= '2'
AND bokningar.weektype = '1'
AND bokningar.Monday = '1' ## Monday this is dynamically changed to current date
AND bokningar.avbokad < '1'
AND tilldelade.bok_id IS NULL
try something like
SELECT ...
FROM TABLEA
WHERE TABLEA.tilldelad=Requested Date
AND NOT EXISTS (SELECT 1 FROM TABLEB
WHERE TABLEB.tilldelad=Requested Date
AND TABLEA.bok_id = TABLEB.bok_id
AND...)
You need to do set operations.
A simple minus Query would solve your problem here.
Select * from tableA
minus
Select * from tableB;
This would display all data in tableA which is not present in tableB.
You can read more about it over here. http://en.wikipedia.org/wiki/Set_operations_%28SQL%29
For your specific case, try this :
Select * from tableA A
minus
Select * from tableB B where B.tilldelad='3013-09-30';

Struggling with long running query even for explain plan also it was taking very much time

My query is taking very long time around 1hr.
2.due to this it was impacting on DB performance.
3.even for Explain plan also it was taking very long time.
4.My query is very big.
Query:
select count(*) from
(
select paid.keyword_id, paid.keyword_name, stat.orgEntrances,
keyword.rank1, keyword.rank_check,
SUM(paid.clicks) as sumclick,
SUM(paid.clicks * paid.avg_position) as sumclickavgpos,
SUM(paid.itemRevenue) as sumitem,
SUM(paid.cost) as sumcost,
SUM(paid.transactions) as sumtrans,
SUM(paid.impressions) as sumimpress,
IF(SUM(paid.impressions) = 0, 0,
SUM(paid.impressions * paid.avg_position) / SUM(paid.impressions))
as sumimpressavgrank,
con.item_revenue, con.transactions, keyword.monthly_search_volume
from `t_keyword_paid_analytics_google_ib` paid
left join
(
select outer_t.keyword_id,
sum(outer_t.item_revenue) as item_revenue,
sum(outer_t.transactions) as transactions
from t_keyword_conversion_ga_ib outer_t
where outer_t.own_domain_id = 720
and outer_t.traffic_date >= '2012-12-01'
and outer_t.traffic_date <= '2012-12-31'
group by outer_t.keyword_id
) con
on paid.keyword_id = con.keyword_id
left join
(
select outer_t.keyword_id, sum(outer_t.entrances) as orgEntrances
from t_keyword_stat_ga_ib outer_t
where outer_t.own_domain_id = 720
and outer_t.traffic_date >= '2012-12-01'
and outer_t.traffic_date <= '2012-12-31'
and (outer_t.medium = 'organic' or outer_t.medium is null )
group by outer_t.keyword_id
) stat
on paid.keyword_id = stat.keyword_id
left join `t_managed_keyword_ib` keyword on keyword.id = paid.keyword_id
where paid.own_domain_id = 720 and paid.traffic_date >= '2012-12-01'
and paid.traffic_date <= '2012-12-31'
and (paid.channel is null or paid.channel = 'Google')
group by paid.keyword_id having paid.keyword_id is not null
) tempt;
Tables Structure:
mysql> show create table t_keyword_paid_analytics_google_ib\G
*************************** 1. row ***************************
Table: t_keyword_paid_analytics_google_ib
Create Table: CREATE TABLE `t_keyword_paid_analytics_google_ib` (
`keyword_name` varchar(255) DEFAULT NULL,
`id` int(11) NOT NULL,
`keyword_id` int(11) NOT NULL,
`target_url_id` int(11) DEFAULT NULL,
`own_domain_id` int(11) NOT NULL,
`log_date` date NOT NULL,
`traffic_date` date NOT NULL,
`impressions` int(11) DEFAULT NULL,
`clicks` int(11) DEFAULT NULL,
`entrances` int(11) DEFAULT NULL,
`match_type` int(11) DEFAULT NULL COMMENT '1: Phrase, 2: Exact, 3:Broad 4: etc',
`ad_group_name` varchar(200) DEFAULT NULL,
`ad_distribution_network` varchar(500) CHARACTER SET latin1 DEFAULT NULL,
`match_query` varchar(500) CHARACTER SET latin1 DEFAULT NULL,
`cost` decimal(10,2) DEFAULT NULL,
`cpm` decimal(10,2) DEFAULT NULL,
`ctr` decimal(10,2) DEFAULT NULL COMMENT 'percent',
`cpc` decimal(10,2) DEFAULT NULL,
`campaign` varchar(200) CHARACTER SET latin1 DEFAULT NULL,
`keyword_status` tinyint(4) DEFAULT NULL COMMENT '1: Active,2: Approved, 3: Disapproved, 4: Paused , 5:Pending, 6: Failed, 7:etc',
`ad_group_status` tinyint(4) DEFAULT NULL COMMENT '1:ELIGIBLE; 2=PAUSED;3=LOW_SEARCH_VOLUME;4 =LOW_QUALITY_SCORE; 5=DISAPPROVED; 6=AD_GROUP_PAUSED; 7=etc',
`max_cpc` decimal(10,2) DEFAULT NULL,
`quality_score` tinyint(4) DEFAULT NULL,
`channel` varchar(100) DEFAULT NULL,
`first_page_cpc` decimal(10,2) DEFAULT NULL,
`avg_position` decimal(10,2) DEFAULT NULL,
`itemRevenue` decimal(10,2) DEFAULT NULL,
`goal1value` decimal(10,2) DEFAULT NULL,
`goal2value` decimal(10,2) DEFAULT NULL,
`goal3value` decimal(10,2) DEFAULT NULL,
`goal4value` decimal(10,2) DEFAULT NULL,
`transactions` int(10) DEFAULT NULL,
`goal1completions` int(10) DEFAULT NULL,
`goal2completions` int(10) DEFAULT NULL,
`goal3completions` int(10) DEFAULT NULL,
`goal4completions` int(10) DEFAULT NULL
) ENGINE=BRIGHTHOUSE DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> show create table t_keyword_conversion_ga_ib\G
*************************** 1. row ***************************
Table: t_keyword_conversion_ga_ib
Create Table: CREATE TABLE `t_keyword_conversion_ga_ib` (
`keyword_name` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`id` int(11) NOT NULL,
`own_domain_id` int(11) DEFAULT NULL,
`keyword_id` int(11) DEFAULT NULL,
`traffic_date` date DEFAULT NULL,
`targeturl_id` int(11) DEFAULT NULL,
`entrance` int(11) DEFAULT NULL,
`transactions` int(11) DEFAULT NULL,
`item_revenue` decimal(9,2) DEFAULT NULL,
`goal1completions` int(11) DEFAULT NULL,
`goal2completions` int(11) DEFAULT NULL,
`goal3completions` int(11) DEFAULT NULL,
`goal4completions` int(11) DEFAULT NULL,
`goal5completions` int(11) DEFAULT NULL,
`goal6completions` int(11) DEFAULT NULL,
`goal7completions` int(11) DEFAULT NULL,
`goal8completions` int(11) DEFAULT NULL,
`goal9completions` int(11) DEFAULT NULL,
`goal10completions` int(11) DEFAULT NULL,
`goal1Value` decimal(9,2) DEFAULT NULL,
`goal2Value` decimal(9,2) DEFAULT NULL,
`goal3Value` decimal(9,2) DEFAULT NULL,
`goal4Value` decimal(9,2) DEFAULT NULL,
`goal5Value` decimal(9,2) DEFAULT NULL,
`goal6Value` decimal(9,2) DEFAULT NULL,
`goal7Value` decimal(9,2) DEFAULT NULL,
`goal8Value` decimal(9,2) DEFAULT NULL,
`goal9Value` decimal(9,2) DEFAULT NULL,
`goal10Value` decimal(9,2) DEFAULT NULL,
`medium` varchar(255) COLLATE latin1_bin DEFAULT NULL,
`source` varchar(255) COLLATE latin1_bin DEFAULT NULL
) ENGINE=BRIGHTHOUSE DEFAULT CHARSET=latin1 COLLATE=latin1_bin
1 row in set (0.00 sec)
Please help me here.
Create index and change where condition. put (outer_t.medium = 'organic' or outer_t.medium is null ) after outer_t.own_domain_id = 720 and (paid.channel is null or paid.channel = 'Google') after paid.own_domain_id = 720. Try, i hope it will make your query faster.

MySQL - Counting a count?

I'm trying to aggregate some data I've pulled from my MySQL db.
Query I'm using is:
SELECT `PUID`,`DROID_V`,`SIG_V`,`SPEED`,
COUNT(distinct IF(sourcelist.hasExtension=1,NAME,NULL)) as Ext,
COUNT(distinct IF(sourcelist.hasExtension=0,NAME,NULL)) as NoExt,
COUNT(distinct NAME) as `All`
FROM sourcelist, main_small
WHERE sourcelist.SourcePUID = 'My_Variable' AND main_small.NAME = sourcelist.SourceFileName
GROUP BY `PUID`,`DROID_V`,`SIG_V`,`SPEED` ORDER BY `DROID_V` ASC, `SIG_V` ASC, `SPEED`
And I wondered if there was a way of counting this result, so I can make a new table that would show me something like:
Every distinct PUID, (count of distinct DROID_V), (count of distinct Sig_V), (SUM of total hits for NAME) WHERE sourcelist.SourcePUID = 'My_Variable' AND main_small.NAME = sourcelist.SourceFileName
As you can see, I'm really not very good at SQL!
Source table:
CREATE TABLE `t1` (
`DROID_V` int(1) DEFAULT NULL,
`Sig_V` varchar(7) DEFAULT NULL,
`SPEED` varchar(4) DEFAULT NULL,
`ID` varchar(7) DEFAULT NULL,
`PARENT_ID` varchar(10) DEFAULT NULL,
`URI` varchar(10) DEFAULT NULL,
`FILE_PATH` varchar(68) DEFAULT NULL,
`NAME` varchar(17) DEFAULT NULL,
`METHOD` varchar(10) DEFAULT NULL,
`STATUS` varchar(14) DEFAULT NULL,
`SIZE` int(10) DEFAULT NULL,
`TYPE` varchar(10) DEFAULT NULL,
`EXT` varchar(4) DEFAULT NULL,
`LAST_MODIFIED` varchar(10) DEFAULT NULL,
`EXTENSION_MISMATCH` varchar(32) DEFAULT NULL,
`MD5_HASH` varchar(10) DEFAULT NULL,
`FORMAT_COUNT` varchar(10) DEFAULT NULL,
`PUID` varchar(15) DEFAULT NULL,
`MIME_TYPE` varchar(24) DEFAULT NULL,
`FORMAT_NAME` varchar(10) DEFAULT NULL,
`FORMAT_VERSION` varchar(10) DEFAULT NULL,
`INDEX` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`INDEX`)
) ENGINE=MyISAM AUTO_INCREMENT=960831 DEFAULT CHARSET=utf8
example records:
5;"v37";"slow";"10266";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/7";"image/tiff";"Tagged Ima";"3";"191977"
5;"v37";"slow";"10268";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/8";"image/tiff";"Tagged Ima";"4";"191978"
5;"v37";"slow";"10269";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/9";"image/tiff";"Tagged Ima";"5";"191979"
5;"v37";"slow";"10270";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/10";"image/tiff";"Tagged Ima";"6";"191980"
Sure just do something like
Select Count(*) From (Select * From SomeTable) adummynamesoSqlParserDoesntgetupset
so put your query in parentheses after FROM give it a unique name, and you can treat it like a table or a view.