Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:39:36+00:00 2026-05-24T09:39:36+00:00

I have an SQL, which takes more than minute to execute, which is too

  • 0

I have an SQL, which takes more than minute to execute, which is too much for me, can someone give advice on optimization.

UPDATE 2 – a little modified sql with increase of speed:

SELECT STRAIGHT_JOIN
kw.`id` ,  kw.`clientid` ,  kw.`day` ,  kw.`campaignid` ,  kw.`campaign` ,  kw.`adgroupid`,  kw.`adgroup` ,  kw.`matchtype` ,  
kw.`firstpagecpc` ,  kw.`keywordtext` ,  kw.`qualityscore` ,  kw.`maxcpc` ,  kw.`avgcpm` ,  kw.`status` ,  kw.`keywordtext` ,  
kw.`matchtype` ,

SUM( kw.`impressions` )                                                  AS impressions,
SUM(  kw.`clicks` )                                                      AS clicks,
SUM(  kw.`impressions` *  kw.`avgposition` ) / SUM(  kw.`impressions` )  AS avgposition, 
SUM(  kw.`cost` )                                                        AS cost,
(SUM(  kw.`clicks` ) / SUM(  kw.`impressions` ) *100)                    AS ctr,
(SUM(  kw.`cost` ) / SUM(  kw.`clicks` ))                                AS avgcpc,
IF(kw.maxcpc = 0,kw.maxcpc,ad.defaultmaxcpc)                             AS maxcpc

FROM  `keywordsreport`                                                   AS kw
JOIN  `adgroupreport`                                                    AS ad 
ON (kw.clientid = ad.clientid AND kw.`day` = ad.day)

WHERE  
kw.`campaignid` = '55347673'
AND kw.`clientid` = '6588826821'
AND kw.`day` BETWEEN '2011-07-01' AND '2011-07-31'
AND (kw.`adid` = '' OR kw.`adid` = '0')
GROUP BY kw.`keywordtext`, kw.`matchtype`

keywordsreport table indexes:

PRIMARY  PRIMARY 235232  id

Unique   UNIQUE  235232  adgroupid
                         day
                         keywordtext
                         matchtype
                         adid
INDEX    INDEX   16802   keywordtext
                          matchtype
                          clientid

CLIENTIDDAY  INDEX   6637 clientid
                          day

adgroupreport table indexes:

PRIMARY          PRIMARY 1328238  id

CLIENTIDDAY  INDEX   6447     clientid
                                  day

UPDATE 3:
Sample query with EXPLAIN:

    i  select_type  table   type    possible_keys                                      key                key_len   ref     rows    Extra
    1   SIMPLE      kw      range   CLIENTID,CLIENTIDDAY,ADIDDAY,CAMPAIGNCLIENTDAY     CAMPAIGNCLIENTDAY  211   NULL    350 Using where; Using temporary; Using filesort
    1   SIMPLE      ad      range   CLIENTIDDAY                                        CLIENTIDDAY        65    NULL    42464   Using where

In each table: keywordsreport AND adgroupreport there could be ~ 1 000 000 records.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:39:37+00:00Added an answer on May 24, 2026 at 9:39 am

    I would go one step further… A client can have many campaigns, so your index should be on the smallest criteria first. I would have an index on

    ( CampaignID, ClientId, Day )
    

    then just for grins to match the anticipated index for optimizing, just adjust the WHERE clause to

    WHERE
             kw.campaignid = '23213231232'
          AND kw.clientid = '$clientid'
          AND kw.`day` BETWEEN '2011-07-31' AND '2011-07-01'
          AND ( kw.adid = '' OR kw.adid = '0')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following query which takes almost 1 minute to execute. public static Func<Entities,
Can we have a SQL query which will basically help in viewing table and
I have an sql query with inner joins of four tables that takes more
hey guys, i have a query in sql server which takes atleast 10-15 seconds
I have two SQL scripts which get called within a loop that accept a
I have a SQL table which has a number of fields ID | Value
I have a sql table which have the following data, Id City Country ---
I have a SQL script which is extremely large (about 700 megabytes). I am
I have a Sql Database (which I have no control over the schema) that
I have an SQL question which may be basic to some but is confusing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.