تنفيذ التقسيم بالتجزئة
تعلّم كيف يوزّع PostgreSQL الصفوف بالتساوي على مجموعة ثابتة من الأقسام باستخدام تجزئة المفتاح، وهو أسلوب مثالي لموازنة الحمل دون وجود نطاقات طبيعية.
تنفيذ التقسيم بالتجزئة درس مجاني في Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Advanced PostgreSQL: Indexing, Partitioning, Replication، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Advanced PostgreSQL: Indexing, Partitioning, Replication 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
When Range and List Do Not Fit
Range partitioning suits dates; list partitioning suits known categories. But what if you just want even distribution with no natural boundaries?
That is where hash partitioning shines.
What Is Hash Partitioning?
Hash partitioning applies a hash function to the partition key and assigns each row to a partition based on the remainder, spreading rows evenly.
Declaring the Parent Table
Create the parent table partitioned BY HASH on a key column.
CREATE TABLE events (
id BIGINT,
user_id BIGINT,
payload TEXT
) PARTITION BY HASH (user_id);Creating Partitions
Each partition declares a modulus and remainder. With modulus 4 you create four partitions, remainders 0 to 3.
CREATE TABLE events_0 PARTITION OF events FOR VALUES WITH (MODULUS 4, REMAINDER 0);The Full Set
You must create partitions covering every remainder, otherwise some rows have nowhere to go.
CREATE TABLE events_1 PARTITION OF events FOR VALUES WITH (MODULUS 4, REMAINDER 1);
CREATE TABLE events_2 PARTITION OF events FOR VALUES WITH (MODULUS 4, REMAINDER 2);
CREATE TABLE events_3 PARTITION OF events FOR VALUES WITH (MODULUS 4, REMAINDER 3);How Rows Get Routed
On insert, PostgreSQL hashes user_id, takes the remainder modulo 4, and stores the row in the matching partition automatically.
Even Distribution
Because hashing scatters values, partitions stay roughly equal in size, avoiding the hot-partition problem that range partitioning can suffer.
Choosing the Modulus
The modulus equals your number of partitions. Pick it up front, since changing partition count later requires redistributing data.
Indexes per Partition
Create indexes on the parent and PostgreSQL propagates them to all partitions, keeping lookups fast within each shard.
CREATE INDEX ON events (user_id);Hash vs Range vs List
- Range: ordered boundaries (dates, numbers)
- List: discrete known values (regions)
- Hash: even spread when no natural grouping exists
Limitations
Hash partitioning gives no partition pruning for range queries on the key, since values are scattered. It helps point lookups and write distribution, not range scans.
Quick Check
Test your hash partitioning knowledge.
Recap
Hash partitioning spreads rows evenly across a fixed number of partitions using MODULUS and REMAINDER on a hashed key.
It is ideal for balancing writes and point lookups when there is no natural range or list, but it cannot prune partitions for range queries on the key.
الأسئلة الشائعة
هل درس «تنفيذ التقسيم بالتجزئة» مجاني؟
نعم — نص درس «تنفيذ التقسيم بالتجزئة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Advanced PostgreSQL: Indexing, Partitioning, Replication، انتقل إلى CoddyKit PRO. تتضمن دورة Advanced PostgreSQL: Indexing, Partitioning, Replication 4 دروس في المجموع.
ماذا ستتعلم في «تنفيذ التقسيم بالتجزئة»؟
تعلّم كيف يوزّع PostgreSQL الصفوف بالتساوي على مجموعة ثابتة من الأقسام باستخدام تجزئة المفتاح، وهو أسلوب مثالي لموازنة الحمل دون وجود نطاقات طبيعية. تتمرن على Advanced PostgreSQL: Indexing, Partitioning, Replication مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Advanced PostgreSQL: Indexing, Partitioning, Replication؟
لا تُشترط خبرة سابقة. Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تنفيذ التقسيم بالتجزئة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Advanced PostgreSQL: Indexing, Partitioning, Replication هذا؟
نعم. كل درس في Advanced PostgreSQL: Indexing, Partitioning, Replication يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا تقسيم الجداول؟
- إعداد التقسيم حسب النطاق
- تطبيق التقسيم حسب القائمة
- تنفيذ التقسيم بالتجزئة