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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:50:37+00:00 2026-06-10T19:50:37+00:00

Suppose I have a table called Projects with a column called Budget with a

  • 0

Suppose I have a table called Projects with a column called Budget with a standard B-Tree index. The table has 50,000 projects, and only 1% of them have a Budget of over one million. If I ran the SQL Query:

SELECT * From Projects WHERE Budget > 1000000;

The planner will use an index range scan on Budget to get the rows off the heap table. However, if I use the query:

SELECT * From Projects WHERE Budget > 50;

The planner will most likely do a sequential scan on the table, as it will know this query will end up returning most or all rows anyway and there’s no reason to load all the pages of the index into memory.

Now, let’s say I run the query:

SELECT * From Projects WHERE Budget > :budget;

Where :budget is a bind parameter passed into my database. From what I’ve read, the query as above will be cached, and no data on cardinality can be inferred. In fact, most databases will just assume an even distribution and the cached query plan will reflect that. This surprised me, as usually when you read about the benefits of bind parameters it’s on the subject of preventing SQL injection attacks.

Obviously, this could improve performance if the resulting query plan would be the same, as a new plan wouldn’t have to be compiled, but could also hurt performance if the values of :budget greatly varied.

My Question: Why are bind parameters not resolved before the query plan is generated and cached? Shouldn’t modern databases strive to generate the best plan for the query, which should mean looking at the value for each parameter and getting accurate index stats?

Note: This question probably doesn’t apply to mySql as mySql doesn’t cache SQL plans. However, I’m interested in why this is the case on Postgres, Oracle and MS SQL.

  • 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-10T19:50:39+00:00Added an answer on June 10, 2026 at 7:50 pm

    This surprised me, as usually when you read about the benefits of bind parameters it’s on the subject of preventing SQL injection attacks.

    Don’t confuse parameterized queries with prepared statements. Both offer parameterization, but prepared statements offer the additional caching of the query plan.

    Why are bind parameters not resolved before the query plan is generated and cached?

    Because sometimes generating the query plan is an expensive step. Prepared statements allow you to amortize the cost of query planning.

    However, if all you’re looking for is SQL injection protection, don’t use prepared statements. Use parameterized queries.

    For example, in PHP, you can use http://php.net/pg_query_params to execute a parameterized query WITHOUT caching the query plan; meanwhile http://php.net/pg_prepare and http://php.net/pg_execute are used to cache a plan for a prepared statement and later execute it.

    Edit: 9.2 apparently changes the way prepared statements are planned

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

Sidebar

Related Questions

Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose I have a table, the first column is an identity. I thought that
Suppose I have a table called 'test', into which there are three columns named
Consider the following data model: Suppose I have a table called SuperAwesomeData where each
Suppose I have a MySQL table called MyTable, that looks like this: +----+------+-------+ |
Suppose I have a massive table called inactiveUsers and a search form. I want
Suppose I have table first_table which has an FK to table second_table table second_table
Suppose I have a table called device as below: device_id(field) 123asf15fas 456g4fd45ww 7861fassd45 I
I have a column A in a table called T . The column is
Suppose I have a SQL table called AT_Devices with each record representing a piece

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.