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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:37:59+00:00 2026-06-17T22:37:59+00:00

I’m working to understand how cost and actual time should be used to optimize

  • 0

I’m working to understand how cost and actual time should be used to optimize queries. My app is rails 3 with a PostgreSQL 9.1 db. My query is used by Delayed_job:

EXPLAIN ANALYZE SELECT  "delayed_jobs".*
FROM "delayed_jobs"
WHERE ((run_at <= '2011-05-23 15:16:43.180810' AND (locked_at IS NULL OR locked_at < '2011-01-25 11:05:28.077144') OR locked_by = 'host:foo pid:2') AND failed_at IS NULL AND queue = 'authentication_emails')
ORDER BY priority ASC, run_at ASC LIMIT 5

Or:

EXPLAIN ANALYZE SELECT  "delayed_jobs".*
FROM "delayed_jobs"
WHERE ((run_at <= '2011-05-23 15:16:43.180810' AND (locked_at IS NULL OR locked_at < '2011-01-25 11:05:28.077144') OR locked_by = 'host:foo pid:2') AND failed_at IS NULL )
ORDER BY priority ASC, run_at ASC LIMIT 5

For the first query, the output equals:

Limit  (cost=7097.57..7097.57 rows=1 width=1008) (actual time=35.657..35.657 rows=0 loops=1)
  ->  Sort  (cost=7097.57..7097.57 rows=1 width=1008) (actual time=35.655..35.655 rows=0 loops=1)
        Sort Key: priority, run_at
        Sort Method: quicksort  Memory: 25kB
        ->  Seq Scan on delayed_jobs  (cost=0.00..7097.56 rows=1 width=1008) (actual time=35.648..35.648 rows=0 loops=1)
              Filter: ((failed_at IS NULL) AND ((queue)::text = 'authentication_emails'::text) AND (((run_at <= '2011-05-23 15:16:43.18081'::timestamp without time zone) AND ((locked_at IS NULL) OR (locked_at < '2011-01-25 11:05:28.077144'::timestamp without time zone))) OR (locked_by = 'host:foo pid:2'::text)))
Total runtime: 35.695 ms

The table currently has 90k records and can range from 0-200k. We’re noticing this query is causing the CPU to spike and cause bottlenecks. What can be learned from the explain info above. Where should indexes be added if any? Thanks

DB Schema.. Table has 0 indexes.

  create_table "delayed_jobs", :force => true do |t|
    t.integer  "priority",   :default => 0
    t.integer  "attempts",   :default => 0
    t.text     "handler"
    t.text     "last_error"
    t.datetime "run_at"
    t.datetime "locked_at"
    t.datetime "failed_at"
    t.text     "locked_by"
    t.datetime "created_at",                :null => false
    t.datetime "updated_at",                :null => false
    t.string   "queue"
  end
  • 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-06-17T22:38:02+00:00Added an answer on June 17, 2026 at 10:38 pm

    Analysis

    If you will go this section of the PostgreSQL documentation, you will learn how planner is using statistics to estimate costs. This is very usable information!

    If you say, that table has round 90k records (and using default costs), then cost for rows’ processing will be:

    90000 * (cpu_tuple_cost + cpu_operator_cost) = 90000 * 0.0125 = 1125
    

    We can now approximate how many pages your table occupies:

    (7097.56-1125)/seq_page_cost = 5972.56
    

    Which makes it roughly 46Mb (with default 8k page size). Thus I assume your table fits into the shared_buffers, even default ones.

    Looking at the average row width I also assume, that table is mostly stored as MAIN.

    Next, you’re using fields of type text and string as predicates. Not sure how they map to the PostgreSQL internal types, but I would assume it is text. This type is compressable by default, therefore PostgreSQL has to perform de-compression for each row to check predicates. I’m not sure after which threshold compression kicks off, take a look at this message (and the whole thread).

    Conclusion

    1. You haven’t shown us real EXPLAIN (analyze) output, as I also don’t think that 35ms query can cause bottlenecks, except…
    2. You haven’t mentioned how many sessions are using your database at the bottleneck moments, and also it is not clear how frequently this query is run. I assume that it is very popular one.
    3. Your table seems to fit into the memory, therefore all operations will be CPU-bound in any case.
    4. Values used in the predicates are compressable and seems to be compressed.

    Therefore I’d said that bottleneck comes from the peak amount of queries run in parallel on the data, that requires extra CPU cycles for de-compression.

    What to do?

    1. Normalise your table. It feels that “queue” column is of very low selectivity. Consider creating external type (like ENUM) for it, or organize a dictionary table with a proper Foreign Key. I’m also not sure bout locked_by column, can it be normalised?
    2. Create indexes on run_at and locked_at columns.
    3. Index ON priority, run_at columns will benefit your sorts, but I doubt it will help in this case. I assume that priority column is of low selectivity, therefore planner will prefer to use Bitmap And over Index Scans on run_at and locked_at columns.

    I hope I am not terribly wrong here 🙂 Comments/corrections are welcome!

    P.S. Let me know how it goes for you.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.