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

  • Home
  • SEARCH
  • 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 6376867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:47:57+00:00 2026-05-25T01:47:57+00:00

I am trying to output the total content views from my stats table and

  • 0

I am trying to output the total content views from my stats table and group by the year… My stats table is INNODB and has 8M lines and growing…

The table is essentially ID, DATE, MAKE, IP, REFERRER (indexes on id,date,make)

Each entry has an auto-incremented ID, the entry date YYYY-MM-DD HH:MM:SS, and a product make like ‘sony’, ‘panasonic’ etc…

I am trying to make a query that does not kill my server that sums up the total content views per year and shows them in order from most viewed to least viewed…(for this year 2011) so that I can use that data to populate a JS chart comparing this year with the past years. I can do this with multiple queries and walking through arrays in PHP but I think there should be a way to get this in one query, but hell if I can figure it out.

Any ideas? Also, am I better to make three independent queries and deal with the results in PHP or can I get this into one query that is more MYSQL efficient.

The output I would like to see (although I cannot seem to make it do this), is simply

MAKE           2009Total   2010Total 2011Total
----           ---------   --------- ---------
Panasonic      800         2345      3456
Sony           998         5346      2956
JVC            1300        1234      1944           

Assume my table has data in it from 2009 to now, I need my array to contain one line per make…

Any help would be appreciated… I am amazed at how fast results like this come back from analytics tools and mine take about 75seconds on 4x Quad-core XEON RAID mysql server… this stats table is not being written to but once a day to dump in the previous day’s stats so I am not sure why my 3 sep queries are so slow… hence my question… maybe a single query won’t be any faster?

Anyway, any help would be appreciated and opinions about speeding up stats queries from a generic view stats table would be welcomed!

  • 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-25T01:47:58+00:00Added an answer on May 25, 2026 at 1:47 am

    I have made an observation. Your query is requesting by year. You should do two things:

    1. store the year
    2. create a better index (product,year)

    Here is how yuou can do so:

    CREATE TABLE stats_entry_new LIKE stats_entry;
    ALTER TABLE stats_entry_new ADD COLUMN entryyear SMALLINT NOT NULL AFTER date;
    ALTER TABLE stats_entry_new ADD INDEX product_year_ndx (product,year);
    ALTER TABLE stats_entry_new DISABLE KEYS;
    INSERT INTO stats_entry_new
    SELECT ID, DATE,YEAR(date),product,IP,REFERRER FROM state_entry;
    ALTER TABLE stats_entry_new ENABLE KEYS;
    ALTER TABLE stats_entry RENAME stats_entry_old;
    ALTER TABLE stats_entry_new RENAME stats_entry;
    

    Now the query looks like this:

    SELECT A.product,B.cnt "2009Total",C.cnt "2010Total",D.cnt "2011Total"
    FROM
    (SELECT DISTINCT product FROM stats_entry) A
    INNER JOIN 
    (SELECT product,COUNT(1) cnt FROM stats_entry WHERE entryyear=2009 GROUP BY product) B
    USING (product)
    (SELECT product,COUNT(1) cnt FROM stats_entry WHERE entryyear=2010 GROUP BY product) C
    USING (product)
    (SELECT product,COUNT(1) cnt FROM stats_entry WHERE entryyear=2011 GROUP BY product) D
    USING (product);
    

    Now to be fair, if you do not want to add a year to the table then you still have to make an index

    ALTER TABLE stats_entry ADD INDEX product_date_ndx (product,date);
    

    Your query looks like this now

    SELECT A.product,B.cnt "2009Total",C.cnt "2010Total",D.cnt "2011Total"
    FROM
    (SELECT DISTINCT product FROM stats_entry) A
    INNER JOIN 
    (SELECT product,COUNT(1) cnt FROM stats_entry
    WHERE date >= '2009-01-01 00:00:00'
    AND date <= '2009-12-31 23:59:59'
    GROUP BY product) B
    USING (product)
    (SELECT product,COUNT(1) cnt FROM stats_entry
    WHERE date >= '2010-01-01 00:00:00'
    AND date <= '2010-12-31 23:59:59'
    GROUP BY product) C
    USING (product)
    (SELECT product,COUNT(1) cnt FROM stats_entry
    WHERE date >= '2011-01-01 00:00:00'
    AND date <= '2011-12-31 23:59:59'
    GROUP BY product) D
    USING (product);
    

    Give it a Try !!!

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

Sidebar

Related Questions

I am trying to capture output from an install script (that uses scp) and
I am trying to change the rows output by PHP in a table to
I'm trying to output a sentence containing 4 variables, with their values emboldened using
I have an XML input file and I'm trying to output the result of
I am accessing a business class using an ObjectDataSource and trying to produce output
I'm trying to skin HTML output which I don't have control over. One of
I'm trying to send the output to the console (or colouredconsole) ... which I'm
I am trying to modify the output stream to search/replace some XHTML tags returned
I'm trying to edit the output joomla main_menu module so I can make a
I'm trying to redirect the java compiler output to a file. I thought it's

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.