إنشاء الفهارس وحذفها
تعلّم أوامر SQL لإنشاء الفهارس والتحقق منها وحذفها، إلى جانب أفضل الممارسات لإدارتها.
إنشاء الفهارس وحذفها درس مجاني في Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Advanced PostgreSQL: Indexing, Partitioning, Replication، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Advanced PostgreSQL: Indexing, Partitioning, Replication 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Index Management: The Basics
Time to get hands-on. This lesson covers the practical SQL to create, verify, and drop indexes — the everyday tools of index management.
Setting Up Our Table
First, let's build a small users table with a few rows to experiment on. Run the code to create and populate it.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE,
city VARCHAR(100)
);
INSERT INTO users (name, email, city) VALUES
('Alice Smith', 'alice@example.com', 'New York'),
('Bob Johnson', 'bob@example.com', 'Los Angeles'),
('Charlie Brown', 'charlie@example.com', 'New York'),
('Diana Prince', 'diana@example.com', 'London');The CREATE INDEX Command
CREATE INDEX name ON table (column) builds an index to speed up queries. PostgreSQL makes it a B-tree by default — no USING BTREE needed.
Creating Your First Index
Let's index the city column so filtering by city gets much faster as the table grows. Run the command below.
CREATE INDEX idx_users_city ON users (city);Verifying Existing Indexes
Did it work? PostgreSQL's pg_indexes system view lists every index in your database — query it to inspect what you just built.
Example: Listing Indexes
Query pg_indexes filtered to the users table to confirm your new index exists. Run it and check the output.
SELECT indexname, tablename, indexdef
FROM pg_indexes
WHERE tablename = 'users';
Composite Indexes
When queries filter on several columns, use a composite index spanning them. Column order matters — match your typical WHERE clause order.
Creating a Composite Index
Here we build a composite index on city and name — handy when you often search by both at once. Then re-run the pg_indexes query.
CREATE INDEX idx_users_city_name ON users (city, name);The DROP INDEX Command
Drop an index when it's unused, slowing writes, or being replaced. The syntax is simple: DROP INDEX index_name.
Example: Dropping an Index
Let's remove the idx_users_city index by name with DROP INDEX, then verify it's gone with the pg_indexes query.
DROP INDEX idx_users_city;Quick Check: Index Commands
Consider a table named products with columns product_id, category, and price.
Recap: Index Management
That's index management: CREATE INDEX to add, pg_indexes to verify, DROP INDEX to remove. Next up: more advanced index types.
الأسئلة الشائعة
هل درس «إنشاء الفهارس وحذفها» مجاني؟
نعم — نص درس «إنشاء الفهارس وحذفها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Advanced PostgreSQL: Indexing, Partitioning, Replication، انتقل إلى CoddyKit PRO. تتضمن دورة Advanced PostgreSQL: Indexing, Partitioning, Replication 4 دروس في المجموع.
ماذا ستتعلم في «إنشاء الفهارس وحذفها»؟
تعلّم أوامر SQL لإنشاء الفهارس والتحقق منها وحذفها، إلى جانب أفضل الممارسات لإدارتها. تتمرن على Advanced PostgreSQL: Indexing, Partitioning, Replication مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Advanced PostgreSQL: Indexing, Partitioning, Replication؟
لا تُشترط خبرة سابقة. Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «إنشاء الفهارس وحذفها»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Advanced PostgreSQL: Indexing, Partitioning, Replication هذا؟
نعم. كل درس في Advanced PostgreSQL: Indexing, Partitioning, Replication يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- أهمية الفهارس
- أساسيات فهرس B-tree
- إنشاء الفهارس وحذفها
- فهارس المفاتيح الفريدة والمفاتيح الأساسية