← Back to MySQL Course | Chapter 19: Advanced & Reference | Lesson 4 of 5

MySQL Performance Tips

Performance tips एक तेज़ race के लिए advice जैसे हैं: EXPLAIN से route check करें, indexes इस्तेमाल करें और सिर्फ ज़रूरी चीज़ माँगें।
Syntax
sql
EXPLAIN SELECT column FROM table_name WHERE column = value;

CREATE INDEX index_name ON table_name (column);

Queries Analyze करने के लिए EXPLAIN इस्तेमाल करना

EXPLAIN किसी query चलने से पहले उसके लिए MySQL का actual execution plan दिखाता है, यह reveal करते हुए कि यह row by row पूरी table scan कर रहा है या efficiently एक index इस्तेमाल कर रहा है — एक धीमी query diagnose करने के लिए सबसे उपयोगी single tool।

उदाहरण: Using EXPLAIN to Analyze Queries

sql
CREATE TABLE orders (id INT, customer_id INT);
EXPLAIN SELECT * FROM orders WHERE customer_id = 5;

Queried Columns पर Indexes Add करना

WHERE clauses, JOIN conditions, या ORDER BY में दिखने वाले columns पर indexes add करना MySQL को पूरी table scan करने के बजाय सीधे relevant rows पर jump करने देता है, किसी book के index की तरह जो आपको हर page पढ़ने से बचाता है।

उदाहरण: Adding Indexes on Queried Columns

sql
CREATE TABLE orders (id INT, customer_id INT, order_date DATE);
CREATE INDEX idx_customer ON orders (customer_id);

SELECT * से बचें

SELECT * table का हर column वापस लाता है चाहे आपको चाहिए या नहीं, network bandwidth और server memory दोनों बर्बाद करते हुए — सिर्फ actually ज़रूरी specific columns list करना queries को leaner और तेज़ रखता है।

उदाहरण: Avoid SELECT *

sql
CREATE TABLE users (id INT, name TEXT, email TEXT, bio TEXT);
-- Wasteful: pulls every column
SELECT * FROM users;
-- Better: only the columns actually needed
SELECT id, name FROM users;

Table Structures Optimize करना

समय के साथ rows delete करना table के storage को unused gaps के साथ fragmented छोड़ सकता है। OPTIMIZE TABLE चलाना उस wasted space reclaim करता है और heavy delete activity वाली tables पर read performance को noticeably बेहतर कर सकता है।

उदाहरण: Optimizing Table Structures

sql
CREATE TABLE logs (id INT, message TEXT);
DELETE FROM logs WHERE id < 100;
OPTIMIZE TABLE logs;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Query Buffers को Effectively इस्तेमाल करना

MySQL हर query पर धीमे disk reads से बचने के लिए frequently accessed data को memory buffers में रखता है, और relevant system variables check करना बता सकता है कि कब वे buffers undersized हैं और आपके workload के लिए tuning चाहिए।

उदाहरण: Using Query Buffers Effectively

sql
SHOW VARIABLES LIKE 'innodb_buffer_pool_size';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बिना EXPLAIN से check किए एक index add करना कि MySQL actually इसे इस्तेमाल करता है।
  2. WHERE में indexed columns पर functions इस्तेमाल करना, जैसे YEAR(order_date) = 2024, जो index इस्तेमाल होने से रोकता है।
  3. बड़े offsets के साथ SELECT * और LIMIT इस्तेमाल करना, जो कई unnecessary rows पढ़ता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.