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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:19:42+00:00 2026-05-12T07:19:42+00:00

Hey all, I’ve got a query in need of optimizing. It works but its

  • 0

Hey all, I’ve got a query in need of optimizing. It works but its a dog, performance wise.

It reads like this:

SELECT  *
FROM    (
        SELECT  *
        FROM    views
        WHERE   user_id = '1'
        ORDER BY
                page DESC
        ) v
GROUP BY
        v.session

I’m tracking views to different pages, and I want to know the highest page per session, in order to know how far they’ve clicked through (they’re required to view every page all the way to the end) in any given session.

Basically what I’m trying to do is ORDER the results before the GROUP. Which the above achieves, at significant cost.

Anyone who can slap me over the head with how to do this? Thanks guys!

Update:

The Explain:

"1" "PRIMARY"   "<derived2>"    "ALL"   \N  \N  \N  \N  "3545"  "Using temporary; Using filesort"

"2" "DERIVED"   "views" "index" \N  "page"  "5" \N  "196168"    "Using where"

The schema:

ID       int(8) unsigned  (NULL)     NO      PRI     (NULL)   auto_increment  select,insert,update,references         
page     int(8)           (NULL)     YES     MUL     (NULL)                   select,insert,update,references         
user_id  int(8)           (NULL)     YES             (NULL)                   select,insert,update,references         
session  int(8)           (NULL)     YES             (NULL)                   select,insert,update,references         
created  datetime         (NULL)     NO                                       select,insert,update,references       

Index Info:

views            0  PRIMARY              1  ID           A               196008    (NULL)  (NULL)          BTREE    

views            1  page                 1  page         A                  259    (NULL)  (NULL)  YES     BTREE 
  • 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-12T07:19:42+00:00Added an answer on May 12, 2026 at 7:19 am

    I’m tracking views to different pages, and I want to know the highest page per session, in order to know how far they’ve clicked through (they’re required to view every page all the way to the end) in any given session.

    Ordering before grouping is a highly unreliable way to do this.

    MySQL extends GROUP BY syntax: you can use ungrouped and unaggregated fields in SELECT and ORDER BY clauses.

    In this case, a random value of page is output per each session.

    Documentation explicitly states that you should never make any assumptions on which value exactly will it be:

    Do not use this feature if the columns you omit from the GROUP BY part are not constant in the group. The server is free to return any value from the group, so the results are indeterminate unless all values are the same.

    However, in practice, the values from the first row scanned are returned.

    Since you are using an ORDER BY page DESC in your subquery, this row happens to be the rows with a maximal page per session.

    You shouldn’t rely on it, since this behaviour is undocumented and if some other row will be returned in next version, it will not be considered a bug.

    But you don’t even have to do such nasty tricks.

    Just use aggregate functions:

    SELECT  MAX(page)
    FROM    views
    WHERE   user_id = '1'
    GROUP BY
            session
    

    This is documented and clean way to do what you want.

    Create a composite index on (user_id, session, page) for the query to run faster.

    If you need all columns from your table, not only the aggregated ones, use this syntax:

    SELECT  v.*
    FROM    (
            SELECT  DISTINCT user_id, session
            FROM    views
            ) vo
    JOIN    views v
    ON      v.id =
            (
            SELECT  id
            FROM    views vi
            WHERE   vi.user_id = vo.user_id
                    AND vi.session = vo.session
            ORDER BY
                    page DESC
            LIMIT 1
            )
    

    This assumes that id is a PRIMARY KEY on views.

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

Sidebar

Related Questions

Hey all i am in need of some help getting my code working correctly
Hey All, I currently use Google's API to include jQuery into my sites like
Hey all i am in need of a little help with figuring out how
Hey all, i am in need of some help with figuring out how to
Hey all i am trying to delete a key from the registery but i
Hey all of you at Stackoverflow. I got this incredibly annoying problem. I try
Hey all- I have looked this up on here and Google but none of
Hey all, this is my query string here: SELECT SUM(Total) as Total, AdministratorCode, SUM(WOD.Quantity)
Hey all is there any way to query a text file using OleDB or
Hey all. Okay here is the situation. I would like to be able to

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.