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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:39:16+00:00 2026-05-19T00:39:16+00:00

I’ve got two queries that seem more or less identical, but they have very

  • 0

I’ve got two queries that seem more or less identical, but they have very different performance and I can’t figure out why. Basically, I’ve got a large table (6M rows) of blogs that I need to lock and process. Originally, the query looked like this:

insert into bloglock
select b.id, [lockid], CURRENT_TIMESTAMP
from blog b
left join bloglock bl
on b.id=bl.blogid
WHERE
State=[somestate]
AND
bl.BlogId IS NULL

The semantics of this are:
Lock a blog by making an entry in the bloglock table consisting of the blog id, a lock id, and the current timestamp. But only do this if the blog state is what we want to lock, and if the blog isn’t already locked (bl.BlogId IS NULL).

This query runs very fast, around 5/100ths of a second.

But then I had to force certain blogs to be processed before others. So I added an integer priority field, an index on this field, and changed the query to:

insert into bloglock
select b.id, [lockid], CURRENT_TIMESTAMP
from blog b
left join bloglock bl
on b.id=bl.blogid
WHERE
State=[somestate]
AND
bl.BlogId IS NULL
order by Priority desc
LIMIT 100;

Same as before, except that it gets the first 100 hundred in order of priority. This query is dog slow, taking around 30 seconds to execute. I can accept that. But the puzzle is that I rewrote it to this:

insert into bloglock
select *
from
(select b.id, [lockid], CURRENT_TIMESTAMP
from blog b
left join bloglock bl
on b.id=bl.blogid
WHERE
State=[somestate]
AND
bl.BlogId IS NULL
order by Priority desc
LIMIT 100) InnerQuery;

And now it runs as fast as before. I don’t get this. It seems like the same query to me, and certainly I would think it would be the same to the optimizer, but instead there’s a 60-fold difference in performance. What’s going on here?

  • 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-19T00:39:17+00:00Added an answer on May 19, 2026 at 12:39 am

    There is no logical reason why the third query should be faster than the second. I believe that you have probably encountered a bug in the query optimization of MySQL. You might consider filing a bug report about this for the MySQL developers.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
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 have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.