QueryHints

Struct QueryHints 

Source
pub struct QueryHints {
    pub indexes: SmallVec<[IndexHint; 4]>,
    pub parallel_workers: Option<u32>,
    pub join_hints: SmallVec<[JoinHint; 4]>,
    pub no_seq_scan: bool,
    pub no_index_scan: bool,
    pub cte_materialized: Option<bool>,
    pub timeout_ms: Option<u64>,
    pub custom: Vec<String>,
}
Expand description

Query plan hints for optimizing complex queries.

These hints are applied to queries to guide the query planner:

  • Index hints to force specific index usage
  • Parallelism settings
  • Join strategies
  • Materialization preferences

§Database Support

Hint TypePostgreSQLMySQLSQLiteMSSQL
Index✅ (GUC)
Parallel
Join
CTE Mat

§Example

use prax_query::db_optimize::QueryHints;
use prax_query::sql::DatabaseType;

let hints = QueryHints::new()
    .index_hint("users_email_idx")
    .parallel(4)
    .no_seq_scan();

let sql = hints.apply_to_query("SELECT * FROM users WHERE email = $1", DatabaseType::PostgreSQL);

Fields§

§indexes: SmallVec<[IndexHint; 4]>

Index hints.

§parallel_workers: Option<u32>

Parallelism level (0 = default, >0 = specific workers).

§join_hints: SmallVec<[JoinHint; 4]>

Join method hints.

§no_seq_scan: bool

Whether to prevent sequential scans.

§no_index_scan: bool

Whether to prevent index scans.

§cte_materialized: Option<bool>

CTE materialization preference.

§timeout_ms: Option<u64>

Query timeout in milliseconds.

§custom: Vec<String>

Custom database-specific hints.

Implementations§

Source§

impl QueryHints

Source

pub fn new() -> Self

Create new empty hints.

Source

pub fn index_hint(self, index_name: impl Into<String>) -> Self

Add an index hint.

Source

pub fn index_hint_for_table( self, table: impl Into<String>, index_name: impl Into<String>, ) -> Self

Add an index hint for a specific table.

Source

pub fn ignore_index(self, index_name: impl Into<String>) -> Self

Ignore a specific index.

Source

pub fn parallel(self, workers: u32) -> Self

Set parallelism level.

Source

pub fn no_parallel(self) -> Self

Disable parallel execution.

Source

pub fn no_seq_scan(self) -> Self

Prevent sequential scans.

Source

pub fn no_index_scan(self) -> Self

Prevent index scans.

Source

pub fn cte_materialized(self, materialized: bool) -> Self

Set CTE materialization preference.

Source

pub fn nested_loop_join(self, tables: Vec<String>) -> Self

Force nested loop join.

Source

pub fn hash_join(self, tables: Vec<String>) -> Self

Force hash join.

Source

pub fn merge_join(self, tables: Vec<String>) -> Self

Force merge join.

Source

pub fn timeout(self, ms: u64) -> Self

Set query timeout.

Source

pub fn custom_hint(self, hint: impl Into<String>) -> Self

Add a custom database-specific hint.

Source

pub fn to_sql_prefix(&self, db_type: DatabaseType) -> String

Generate hints as SQL prefix for the given database.

Source

pub fn to_sql_suffix(&self, db_type: DatabaseType) -> String

Generate hints as SQL suffix (for query options).

Source

pub fn apply_to_query(&self, query: &str, db_type: DatabaseType) -> String

Apply hints to a query.

Source

pub fn has_hints(&self) -> bool

Check if any hints are configured.

Trait Implementations§

Source§

impl Clone for QueryHints

Source§

fn clone(&self) -> QueryHints

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for QueryHints

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for QueryHints

Source§

fn default() -> QueryHints

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more