[SQL hints Comparison]

Darcy Daeseok YU ·2024년 9월 1일

Here is a detailed comparison of query hints between MySQL, MariaDB, and Oracle. These hints are tools used to influence the query optimizer’s decisions in each database system.

Query Hints Comparison

Hint TypeMySQLMariaDBOracleDescription
Index UsageUSE INDEXUSE INDEXINDEXSuggests using a specific index.
IGNORE INDEXIGNORE INDEXNO_INDEXInstructs to ignore a specific index.
FORCE INDEXFORCE INDEXINDEXForces the use of a specific index.
Join OrderSTRAIGHT_JOINSTRAIGHT_JOINORDEREDForces join order to be as written in the query.
Join MethodsN/AUSE_MERGEUSE_MERGESuggests using a merge join.
N/AUSE_HASHUSE_HASHSuggests using a hash join.
N/AUSE_NLUSE_NLSuggests using a nested loop join.
Parallel ExecutionN/AN/APARALLEL, NO_PARALLELControls parallel execution.
Table ScansN/AN/AFULL, NO_FULLForces or prevents a full table scan.
Query CachingSQL_NO_CACHESQL_NO_CACHENO_RESULT_CACHEPrevents results from being stored in the result cache.
Priority ExecutionHIGH_PRIORITYHIGH_PRIORITYN/AGives the query high priority over others.
Buffering ResultsSQL_BUFFER_RESULTSQL_BUFFER_RESULTN/AForces the server to buffer the results of the query.
Result Size HintSQL_BIG_RESULTSQL_BIG_RESULTN/AInforms the optimizer that the result set will be large.
SQL_SMALL_RESULTSQL_SMALL_RESULTN/AInforms the optimizer that the result set will be small.
Row CalculationSQL_CALC_FOUND_ROWSSQL_CALC_FOUND_ROWSN/ATells the server to calculate the total number of rows without LIMIT.
Join CacheN/ASQL_NO_JOIN_CACHENO_MERGE, NO_PUSH_SUBQPrevents the optimizer from caching join results.
Optimizing for Row RetrievalHIGH_PRIORITYHIGH_PRIORITYFIRST_ROWS, ALL_ROWSOptimizer mode hints for prioritizing either quick retrieval of first rows or all rows.
Leading Table in JoinsN/AN/ALEADINGSuggests the leading table in a join.
Driving SiteN/AN/ADRIVING_SITESpecifies which table in a distributed query should drive the execution.
Transformation RulesN/AN/ASTAR_TRANSFORMATION, NO_STAR_TRANSFORMATIONControls star transformation for star schemas.
Subquery OptimizationN/AN/APUSH_SUBQ, NO_PUSH_SUBQControls subquery pushdown optimization.
Row PrefetchingN/AN/APREFETCH, NO_PREFETCHControls the prefetching of rows for queries.
Direct-Path InsertN/AN/AAPPENDControls direct-path insert.
Result CachingN/AN/ARESULT_CACHE, NO_RESULT_CACHEControls result caching for the query.

Key Points:

  1. MySQL and MariaDB:

    • MySQL and MariaDB share many of the same hints because MariaDB was originally forked from MySQL. However, MariaDB has added some hints that are not present in MySQL, particularly related to join methods like USE_MERGE, USE_HASH, and USE_NL.
    • Both databases offer basic hints like USE INDEX, IGNORE INDEX, and FORCE INDEX for index control, and STRAIGHT_JOIN for join order enforcement.
    • Parallel execution hints and more advanced query manipulation hints (e.g., for star schemas) are not available in MySQL/MariaDB.
  2. Oracle:

    • Oracle offers a much more extensive and flexible set of hints compared to MySQL/MariaDB. These include parallel execution control (PARALLEL), specific join method hints (USE_NL, USE_MERGE, USE_HASH), and query transformation hints (STAR_TRANSFORMATION, PUSH_SUBQ).
    • Oracle hints can also control specific execution details like caching (RESULT_CACHE), prefetching (PREFETCH), and optimizer modes (ALL_ROWS, FIRST_ROWS).

Summary

  • MySQL/MariaDB: Focus primarily on index usage and join order hints. They offer fewer advanced query optimization and execution hints compared to Oracle.
  • Oracle: Provides a comprehensive set of hints that give fine-grained control over nearly all aspects of query execution, from parallelism to result caching, making it much more flexible for complex queries and large-scale applications.
profile
React, React-Native https://darcyu83.netlify.app/

0개의 댓글