السرعة والفهارس / Speed and indexes

الترتيب / Order
4 من 4 / 4 of 4
آخر تحديث / Updated
8 أكتوبر 2026 / 8 October 2026

أهداف الدرس

  1. أن تعرف ما يفعله الفهرس.
  2. أن تتجنّب ما يمنع استعماله.

الفهرس كفهرس الكتاب: بدل قراءة كل الصفحات تذهب مباشرة إلى ما تريد.

SQL
CREATE INDEX employees_name_ix ON employees (name);

-- Uses the index:
SELECT id, name FROM employees WHERE name = :name;

-- Cannot use it: the column is wrapped in a function
SELECT id, name FROM employees WHERE UPPER(name) = 'SAMI';

دالة على العمود في WHERE تمنع استعمال فهرسه، فيُقرأ الجدول كله.

  • قيم مربوطة :name بدل لصق القيم في النص.
  • اختر الأعمدة التي تحتاجها، لا *.
  • فهرس لكل عمود ربط كثير الاستعمال.

تمرين

لماذا القيم المربوطة أفضل؟

الإجابة

لأنها أسرع (تُحلَّل الجملة مرة واحدة) وتمنع حقن SQL.

Lesson goals

  1. Know what an index does.
  2. Avoid what stops it being used.

An index is like a book's index: instead of reading every page you go straight to what you need.

SQL
CREATE INDEX employees_name_ix ON employees (name);

-- Uses the index:
SELECT id, name FROM employees WHERE name = :name;

-- Cannot use it: the column is wrapped in a function
SELECT id, name FROM employees WHERE UPPER(name) = 'SAMI';

A function on the column in WHERE stops its index from being used, so the whole table is read.

  • Bound values :name instead of pasting values into the text.
  • Select the columns you need, not *.
  • An index on every busy join column.

Exercise

Why are bound values better?

Answer

They are faster (the statement is parsed once) and they prevent SQL injection.

التعليقات / Comments

لا تعليقات بعد. كن أول من يسأل. / No comments yet. Be the first to ask.

تعليق الطالب يظهر فوراً، وتعليق الزائر بعد موافقة المدرّس. / A student's comment shows at once; a visitor's once the teacher approves it.

في «قواعد البيانات وSQL» / In “Databases and SQL”

  1. 1الاستعلام والربط / Queries and joinsفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  2. 2المعاملات / Transactionsفيها كود / has codeالتعليقات مفتوحة / comments open
  3. 3تصميم الجداول / Designing tablesفيها كود / has codeالتعليقات مفتوحة / comments open
  4. 4السرعة والفهارس / Speed and indexesفيها كود / has codeالتعليقات مفتوحة / comments open

↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق↑ ↓ to move · Enter to open · Esc to close