Top 20 Proven Techniques to Optimize Your Database Performance
This article compiles essential database optimization strategies—including proper index creation, composite and short indexes, avoiding full‑table scans, efficient use of temporary tables and cursors, and best practices for query writing—to dramatically improve query speed and overall system throughput.
1. Create Indexes
Indexes are vital for query‑heavy applications; missing or ineffective indexes cause full table scans and severe performance degradation. Over‑indexing low‑cardinality columns (e.g., gender) can slow updates and offers no benefit.
2. Composite Indexes
When a query filters on multiple columns, a composite index (e.g., (area, age, salary)) can be used, following the left‑most prefix rule. Place the most selective columns first to maximize efficiency.
3. Indexes Exclude NULL Columns
Columns that contain NULL values are not indexed; if any component of a composite index is NULL, the entire index becomes ineffective. Avoid default NULL values in schema design.
4. Use Short Indexes
For long string columns, index only a prefix (e.g., the first 10‑20 characters) to improve query speed while saving disk space and I/O.
5. Ordering and Indexes
MySQL uses only one index per query; if ORDER BY columns are not part of the chosen index, sorting occurs without index assistance. Prefer composite indexes that cover both WHERE and ORDER BY clauses.
6. Avoid Full Table Scans
Avoid NULL checks in WHERE clauses; set default values (e.g., 0) instead of NULL.
Do not use operators like !=, <>, >=, <=, >, < in WHERE clauses when possible.
Avoid OR conditions; rewrite as separate queries combined with UNION ALL.
Prefer BETWEEN over IN for continuous numeric ranges.
Use left‑anchored LIKE patterns (e.g., name like 'abc%') instead of leading wildcards.
Force index usage when the optimizer skips it: SELECT id FROM t WITH (INDEX(index_name)) WHERE num = @num; Avoid functions, arithmetic, or expressions on indexed columns in WHERE clauses; rewrite them to use the column directly.
Replace IN with EXISTS for better performance.
7. Index Design Considerations
Highly repetitive column values reduce index usefulness; assess cardinality before indexing.
Limit the total number of indexes per table (generally no more than six) to balance read and write performance.
Be cautious with clustered indexes on columns that change frequently, as updates may require costly row reordering.
Prefer numeric data types over character types for fields that store only numbers.
Use CHAR for fixed‑length strings when appropriate, as it can be faster than VARCHAR.
Always select specific columns instead of SELECT * to reduce unnecessary data transfer.
8. Temporary Table & Cursor Optimization
Minimize creation and deletion of temporary tables to reduce system‑table overhead.
Use temporary tables judiciously; for one‑time large data loads, consider SELECT INTO instead of CREATE TABLE followed by many inserts.
Explicitly drop or truncate temporary tables at the end of stored procedures.
Avoid cursors for large result sets (>10,000 rows); rewrite using set‑based operations.
When a cursor is necessary, use FAST_FORWARD for small datasets.
Set SET NOCOUNT ON at the start of stored procedures and triggers to suppress unnecessary DONE_IN_PROC messages.
Limit transaction size and avoid returning massive result sets to improve concurrency.
Consider table variables as an alternative to temporary tables, keeping in mind their limited indexing capabilities.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
