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 Type | PostgreSQL | MySQL | SQLite | MSSQL |
|---|---|---|---|---|
| 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: boolWhether to prevent sequential scans.
no_index_scan: boolWhether 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
impl QueryHints
Sourcepub fn index_hint(self, index_name: impl Into<String>) -> Self
pub fn index_hint(self, index_name: impl Into<String>) -> Self
Add an index hint.
Sourcepub fn index_hint_for_table(
self,
table: impl Into<String>,
index_name: impl Into<String>,
) -> Self
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.
Sourcepub fn ignore_index(self, index_name: impl Into<String>) -> Self
pub fn ignore_index(self, index_name: impl Into<String>) -> Self
Ignore a specific index.
Sourcepub fn no_parallel(self) -> Self
pub fn no_parallel(self) -> Self
Disable parallel execution.
Sourcepub fn no_seq_scan(self) -> Self
pub fn no_seq_scan(self) -> Self
Prevent sequential scans.
Sourcepub fn no_index_scan(self) -> Self
pub fn no_index_scan(self) -> Self
Prevent index scans.
Sourcepub fn cte_materialized(self, materialized: bool) -> Self
pub fn cte_materialized(self, materialized: bool) -> Self
Set CTE materialization preference.
Sourcepub fn nested_loop_join(self, tables: Vec<String>) -> Self
pub fn nested_loop_join(self, tables: Vec<String>) -> Self
Force nested loop join.
Sourcepub fn merge_join(self, tables: Vec<String>) -> Self
pub fn merge_join(self, tables: Vec<String>) -> Self
Force merge join.
Sourcepub fn custom_hint(self, hint: impl Into<String>) -> Self
pub fn custom_hint(self, hint: impl Into<String>) -> Self
Add a custom database-specific hint.
Sourcepub fn to_sql_prefix(&self, db_type: DatabaseType) -> String
pub fn to_sql_prefix(&self, db_type: DatabaseType) -> String
Generate hints as SQL prefix for the given database.
Sourcepub fn to_sql_suffix(&self, db_type: DatabaseType) -> String
pub fn to_sql_suffix(&self, db_type: DatabaseType) -> String
Generate hints as SQL suffix (for query options).
Sourcepub fn apply_to_query(&self, query: &str, db_type: DatabaseType) -> String
pub fn apply_to_query(&self, query: &str, db_type: DatabaseType) -> String
Apply hints to a query.
Trait Implementations§
Source§impl Clone for QueryHints
impl Clone for QueryHints
Source§fn clone(&self) -> QueryHints
fn clone(&self) -> QueryHints
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for QueryHints
impl Debug for QueryHints
Source§impl Default for QueryHints
impl Default for QueryHints
Source§fn default() -> QueryHints
fn default() -> QueryHints
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for QueryHints
impl RefUnwindSafe for QueryHints
impl Send for QueryHints
impl Sync for QueryHints
impl Unpin for QueryHints
impl UnwindSafe for QueryHints
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more