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 6627491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:01:15+00:00 2026-05-25T22:01:15+00:00

I’m wondering if indexes will speed this query up. It takes 9 seconds last

  • 0

I’m wondering if indexes will speed this query up. It takes 9 seconds last time I checked. The traffic table has about 300k rows, listings and users 5k rows. I’m open to ridicule/humiliation too, if this is just a crappy query altogether. I wrote it long ago.

It’s supposed to get the listings with the most page views (traffic). Let me know if the explanation is lacking.

SELECT traffic_listingid AS listing_id, 
       COUNT(traffic_listingid) AS genuine_hits, 
       COUNT(DISTINCT traffic_ipaddress) AS distinct_ips, 
       users.username, 
       listings.listing_address,
       listings.datetime_created,
       DATEDIFF(NOW(), listings.datetime_created) AS listing_age_days 
FROM traffic 
  LEFT JOIN listings 
    ON traffic.traffic_listingid = listings.listing_id 
  LEFT JOIN users 
    ON users.id = listings.seller_id 
WHERE traffic_genuine = 1 
  AND listing_id IS NOT NULL 
  AND username IS NOT NULL 
  AND DATEDIFF(NOW(), traffic_timestamp) < 24 
GROUP BY traffic_listingid 
ORDER BY distinct_ips DESC
LIMIT 10

P.S.

ENGINE=MyISAM /
MySQL Server 4.3
  • 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-25T22:01:16+00:00Added an answer on May 25, 2026 at 10:01 pm

    Sidenotes:

    1.You have

      LEFT JOIN listings 
        ON traffic.traffic_listingid = listings.listing_id 
      ...
    WHERE ... 
      AND listing_id IS NOT NULL 
    

    This condition cancels the LEFT JOIN. Change your query into:

      INNER JOIN listings 
        ON traffic.traffic_listingid = listings.listing_id 
    

    and remove the listing_id IS NOT NULL from the WHERE conditions.

    The same thing applies to LEFT JOIN user and username IS NOT NULL.


    2.The check on traffic_timestamp:

          DATEDIFF(NOW(), traffic_timestamp) < 24
    

    makes it difficult for the index to be used. Change it into something like this that can use an index
    (and check that my version is equivalent, I may have mistakes):

          traffic_timestamp >= CURRENT_DATE() - INTERVAL 23 DAY
    

    3.The COUNT(non-nullable-column) is equivalent to COUNT(*). Change the:

       COUNT(traffic_listingid) AS genuine_hits, 
    

    to:

       COUNT(*) AS genuine_hits, 
    

    because it’s bit faster in MySQL (although I’m not sure about that for version 4.3)


    For the index question, you should have at least an index on every column that is used for joining. Adding one more for the traffic_timestamp will probably help, too.

    If you tell us in which tables the traffic_ipaddress and traffic_timestamp are, and what the EXPLAIN EXTENDED shows, someone may have a better idea.

    Reading again the query, it seems that it’s actually a GROUP BY only in table traffic and the other 2 tables are used to get refrence data. So, the query is equivalent to a (traffic group by)-join-listing-join-user. Not sure if that helps in your MySQL old version but it may be good to have both versions of the query and test if one query runs faster in your system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.