การเลือกชนิดข้อมูลที่เหมาะสม
เลือกชนิดข้อมูลที่มีประสิทธิภาพที่สุดสำหรับคอลัมน์ เพื่อใช้พื้นที่จัดเก็บให้น้อยลงและเพิ่มประสิทธิภาพการประมวลผลคำสั่งค้นหา
การเลือกชนิดข้อมูลที่เหมาะสม เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Data Types Matter
Choosing the correct data type for your columns in PostgreSQL is a fundamental step in designing an efficient database.
It impacts storage, query performance, and data integrity. Selecting the right type ensures your data is stored efficiently and processed quickly.
Picking Integer Types
PostgreSQL offers several integer types, each with a different storage size and value range:
SMALLINT: 2 bytes, range -32,768 to +32,767INTEGER(orINT): 4 bytes, range -2,147,483,648 to +2,147,483,647BIGINT: 8 bytes, range -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
Always choose the smallest integer type that can safely store your expected data range to minimize disk space and improve performance.
Integer Type Demo
Let's see how different integer types are defined. Notice how we pick the smallest type that fits the value.
CREATE TABLE product_counts (
small_count SMALLINT,
medium_count INTEGER,
large_count BIGINT
);
INSERT INTO product_counts (small_count, medium_count, large_count)
VALUES (100, 50000, 1000000000);
SELECT * FROM product_counts;Numeric Precision
When dealing with decimal numbers, especially monetary values, precision is key:
NUMERIC(p, s): Stores exact numbers.pis the total number of digits (precision),sis the number of digits after the decimal point (scale). Essential for financial data.REAL(4 bytes) andDOUBLE PRECISION(8 bytes): Store approximate floating-point numbers. They are faster but can introduce tiny rounding errors, making them unsuitable for money.
Use NUMERIC when exactness is critical; use REAL or DOUBLE PRECISION for scientific or less critical calculations where approximation is acceptable.
Text Storage Choices
For storing text strings, PostgreSQL offers:
VARCHAR(n): Variable-length string with a user-defined maximum lengthn. If you try to insert a longer string, it will be truncated or an error will occur.TEXT: Variable-length string with no explicit maximum length. It's often the most flexible choice.CHAR(n): Fixed-length string. If the string is shorter thann, it's padded with spaces. Generally discouraged due to potential performance issues and space waste.
For most modern applications, VARCHAR (without a length, acting like TEXT) or simply TEXT are preferred for their flexibility and efficient storage.
VARCHAR vs. TEXT
Here's an example demonstrating VARCHAR with a length constraint and TEXT without one. Both store data efficiently based on actual length.
CREATE TABLE messages (
short_msg VARCHAR(50),
long_msg TEXT
);
INSERT INTO messages (short_msg, long_msg)
VALUES ('Hello World', 'This is a much longer message that can span multiple lines and characters.');
SELECT short_msg, LENGTH(short_msg), long_msg, LENGTH(long_msg) FROM messages;Handling Dates & Times
PostgreSQL provides robust date/time types:
DATE: Stores date only (year, month, day).TIME: Stores time of day only (hour, minute, second, fractional seconds).TIMESTAMP: Stores date and time. It does NOT store timezone information.TIMESTAMPTZ(TIMESTAMP WITH TIME ZONE): Stores date and time, and converts it to UTC upon storage. It's the recommended type for most applications to avoid timezone headaches.
Always prefer TIMESTAMPTZ for event timestamps unless you have a very specific reason not to.
Special Types: Boolean & UUID
Two other useful data types:
BOOLEAN: Stores true/false values. It's highly efficient, requiring only 1 byte of storage. PostgreSQL accepts 'true', 'false', 't', 'f', '1', '0', 'yes', 'no' as inputs.UUID: Stores Universally Unique Identifiers. These are 128-bit quantities, useful for generating unique primary keys without database sequence contention, especially in distributed systems.
UUIDs can be generated by PostgreSQL using functions like gen_random_uuid() from the pgcrypto extension.
Type Conversion Overhead
While PostgreSQL often handles implicit type conversions (e.g., converting a string '123' to an integer), this comes with a performance cost.
When you compare or join columns of different data types, PostgreSQL might need to perform a conversion on one or both sides, which can prevent indexes from being used and slow down queries.
Always strive to compare and join columns that have identical data types. If conversion is necessary, use explicit casting (e.g., column::INTEGER) to make it clear and sometimes help the query planner.
Data Type Challenge
Imagine you are designing a table to store user registration details. One column needs to store whether a user has verified their email, and another needs to store a unique, globally identifiable user ID that can be generated anywhere.
Data Type Summary
We've covered the importance of choosing appropriate data types for performance, storage, and integrity.
- Choose the smallest integer type that fits your data.
- Use
NUMERICfor exact decimal values (like money). - Prefer
TIMESTAMPTZfor storing dates and times with timezone awareness. BOOLEANis best for true/false flags, andUUIDfor globally unique identifiers.- Avoid unnecessary type conversions to maintain query performance.
Careful data type selection is a cornerstone of efficient database design.
คำถามที่พบบ่อย
บทเรียน “การเลือกชนิดข้อมูลที่เหมาะสม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเลือกชนิดข้อมูลที่เหมาะสม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเลือกชนิดข้อมูลที่เหมาะสม”
เลือกชนิดข้อมูลที่มีประสิทธิภาพที่สุดสำหรับคอลัมน์ เพื่อใช้พื้นที่จัดเก็บให้น้อยลงและเพิ่มประสิทธิภาพการประมวลผลคำสั่งค้นหา คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การเลือกชนิดข้อมูลที่เหมาะสม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม
ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ข้อแลกเปลี่ยนระหว่างการทำให้เป็นรูปแบบมาตรฐานและการลดรูปแบบมาตรฐาน
- การเลือกชนิดข้อมูลที่เหมาะสม
- การแบ่งพาร์ติชันตารางขนาดใหญ่
- การออกแบบคีย์หลักและคีย์ตัวแทน