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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:27:30+00:00 2026-05-16T18:27:30+00:00

I use the following code to select popular news entries (by date) from the

  • 0

I use the following code to select popular news entries (by date) from the database:

popular = Entry.objects.filter(type='A', is_public=True).extra(select = {'dpub': 'date(dt_published)'}).order_by('-dpub', '-views', '-dt_written', 'headline')[0:5]

To compare the execution speeds of a normal query and this one I ran the following mysql queries:

SELECT *, date(dt_published) as dpub FROM `news_entry` order by dpub DESC LIMIT 500

# Showing rows 0 - 29 (500 total, Query took 0.1386 sec)

–

SELECT * , DATE( dt_published ) AS dpub FROM  `news_entry` ORDER BY id DESC LIMIT 500

# Showing rows 0 - 29 (500 total, Query took 0.0021 sec) [id: 58079 - 57580]

As you can see the normal query is much faster. Is there a way to speed this up?

Is it possible to use mysql views with django?

I realize I could just split the datetime field into two fields (date and time), but I’m curious.


Structure:

CREATE TABLE IF NOT EXISTS `news_entry` (
  `id` int(11) NOT NULL DEFAULT '0',
  `views` int(11) NOT NULL,
  `user_views` int(11) NOT NULL,
  `old_id` int(11) DEFAULT NULL,
  `type` varchar(1) NOT NULL,
  `headline` varchar(256) NOT NULL,
  `subheadline` varchar(256) NOT NULL,
  `slug` varchar(50) NOT NULL,
  `category_id` int(11) DEFAULT NULL,
  `is_public` tinyint(1) NOT NULL,
  `is_featured` tinyint(1) NOT NULL,
  `dt_written` datetime DEFAULT NULL,
  `dt_modified` datetime DEFAULT NULL,
  `dt_published` datetime DEFAULT NULL,
  `author_id` int(11) DEFAULT NULL,
  `author_alt` varchar(256) NOT NULL,
  `email_alt` varchar(256) NOT NULL,
  `tags` varchar(255) NOT NULL,
  `content` longtext NOT NULL
) ENGINE=MyISAM DEFAULT;
  • 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-16T18:27:30+00:00Added an answer on May 16, 2026 at 6:27 pm
    SELECT *, date(dt_published) as dpub FROM `news_entry` order by dpub DESC LIMIT 500
    

    This query orders on dpub, while this one:

    SELECT * , DATE( dt_published ) AS dpub FROM  `news_entry` ORDER BY id DESC LIMIT 500
    

    orders on id.

    Since id is most probably a PRIMARY KEY for your table, and each PRIMARY KEY has an implicit index backing it, ORDER BY does not need to sort.

    dpub is a computed field and MySQL does not support indexes on computed fields. However, ORDER BY dt_published is an ORDER BY dpub as well.

    You need to change your query to this:

    SELECT *, date(dt_published) as dpub FROM `news_entry` order by date_published DESC LIMIT 500
    

    and create an index on news_entry (dt_published).

    Update:

    Since DATE is a monotonic function, you may employ this trick:

    SELECT  *, DATE(dt_published) AS dpub
    FROM    news_entry
    WHERE   dt_published >=
            (
            SELECT  md
            FROM    (
                    SELECT  DATE(dt_published) AS md
                    FROM    news_entry
                    ORDER BY
                            dt_published DESC
                    LIMIT 499, 1
                    ) q
            UNION ALL
            SELECT  DATE(MIN(dt_published))
            FROM    news_entry
            LIMIT 1
            )
    ORDER BY
            dpub DESC, views DESC, dt_written DESC, headline
    LIMIT 500
    

    This query does the following:

    • Selects the 500th record in dt_published DESC order, or the first record posted should there be less than 500 records in the table.

    • Fetches all records posted later than the date of the last record selected. Since DATE(x) is always less or equal to x, there can be more than 500 records, but still
      much less than the whole table.

    • Orders and limits these records as appropriate.

    You may find this article interesting, since it covers a similar problem:

    • Things SQL needs: sargability of monotonic functions
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is about memory use and objects in actionscript 2. If I have
Let say I have the following desire, to simplify the IConvertible's to allow me
I need to solve the following question which i can't get to work by
I want to use a temp directory that will be unique to this build.
(please excuse that I didn't use aliases). I would like my query output to
I make a distributed embedded application that will make use of several micro-controllers. The
I need to develop a file indexing application in python and wanted to know
I am learning on my own about writing an interpreter for a programming language,
I have a new web app that is packaged as a WAR as part
We manage a site for a medical charity. They have a number of links

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.