أهداف الدرس
- أن تعرف ما يفعله الفهرس.
- أن تتجنّب ما يمنع استعماله.
الفهرس كفهرس الكتاب: بدل قراءة كل الصفحات تذهب مباشرة إلى ما تريد.
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
- Know what an index does.
- 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
:nameinstead 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.