ក្នុងបរិស្ថាន "បច្ចេកវិទ្យាធំបំផុត" (អ្នកដឹងថាជាប្រភេទដែលមានម៉ោងនៃអ្នកប្រើប្រាស់, ប្រព័ន្ធទិន្នន័យធំទូលំទូលាយនិងតម្រូវការដែលអភិវឌ្ឍយ៉ាងឆាប់រហ័ស) ការផ្លាស់ប្តូរទិន្នន័យUNIQUE INDEX
គោលបំណងដើម្បីការពារទិន្នន័យចម្លង - ប្រសិនបើវាជាសម្រាប់អ្វីមួយដូចជាការសន្សំសេដ្ឋកិច្ចដែលគ្រប់គ្រងគួរតែត្រឹមត្រូវ - ជាអស្ចារ្យ, អាចមិនមានប្រសិទ្ធិភាពដូចដែលអ្នកគិត។ លើសពីនេះទៀត, តម្លៃនៃការរក្សាទុកវាអាចមានកម្រិតខ្ពស់យ៉ាងអស្ចារ្យ។ គោលបំណងល្អបំផុតគឺជាទូទៅដើម្បីដំណើរការជាច្រើននៃលក្ខណៈពិសេសនៃការ deduplication នៅកម្រិតកម្មវិធី។ ប្រសិនបើអ្នកអាចដោះស្រាយប្រសិនបើអ្នកអាចដោះស្រាយប្រសិនបើអ្នកអាចធ្វើដូច្នេះឬប្រសិនបើអ្នកគិតថាវាជាការរចនាវាយ៉ាងហោចណាស់មុនពេលអនុវត្តមួយ។
ហេតុអ្វីបានជាខ្ញុំបានចាប់ផ្តើមគិតច្នៃប្រឌិតច្នៃប្រឌិតច្នៃប្រឌិតច្នៃប្រឌិតច្នៃប្រឌិត?
Database unique indexes sound pretty reliable, right? The last line of defense against data duplication. I used to think so too. Whenever a field in a table needed to be unique, I'd casually slap a unique index on it.
Until reality gave me a harsh wake-up call.
មុនពេលវេលាជាមុន, នៅពេលដែលខ្មៅរបស់ខ្ញុំគឺមានច្រើនជាងមុន, ខ្ញុំត្រូវបន្ថែមឧបករណ៍បញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូលគ្នានៃការរួមបញ្ចូល។tenant_id
និងis_deleted
ដូច្នេះវាគឺជាការច្នៃប្រឌិតយ៉ាងងាយស្រួលណាស់ទេ? ដូច្នេះវាគឺជាការច្នៃប្រឌិតច្នៃប្រឌិតយ៉ាងងាយស្រួលណាស់។ថ្ងៃក្នុងអំឡុងពេលនេះ, ការបកប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រែប្រ
បន្ទាប់មកមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើមានប្រសិនបើuser@example.com
និងUSER@EXAMPLE.COM
មានប្រសិទ្ធិភាពអ៊ីម៉ែលដូចគ្នា។ លេខកូដកម្មវិធីរបស់អ្នកនឹងប្រសិនបើមានប្រសិនបើមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើវាមានប្រសិនបើ
ទោះជាយ៉ាងណាក៏ដោយប្រសិនបើ "អ៊ីម៉ែលតែមួយគត់" គឺជាការពិតប្រាកដមុនពេលនោះទេប៉ុន្តែឥឡូវនេះតម្រូវការនេះបានផ្លាស់ប្តូរទៅ "អ៊ីម៉ែលតែមួយគត់ + អ៊ីម៉ែលតែមួយគត់" ។ អស្ចារ្យ។ លេខកូដកម្មវិធីគួរតែផ្លាស់ប្តូរបានទេ? និងឧបករណ៍បញ្ជាតែមួយគត់នៃទិន្នន័យទិន្នន័យគួរតែត្រូវបានDROP
កុំព្យូទ័រនិងមួយថ្មីCREATE
d. តើអ្នកធ្វើដូចម្តេចដើម្បីដោះស្រាយប្រព័ន្ធប្រតិបត្តិការទាំងពីរនេះ? អ្វីដែលបានធ្វើជាលើកដំបូង? តើប្រសិនបើអ្វីមួយគត់បានជួសជុលនោះទេ? ការធ្វើប្រព័ន្ធប្រតិបត្តិការដូច្នេះនៅលើបណ្តាញធំទូលំទូលាយគឺដូចជាការដោះស្រាយបូមមួយបន្ទាប់មក – ជាការកាត់បន្ថយអារម្មណ៍យ៉ាងខ្លាំង។
មានបទពិសោធន៍ទាំងនេះធ្វើឱ្យខ្ញុំគួរឱ្យចាប់អារម្មណ៍: នៅក្នុងបរិស្ថានដែលមានទំហំទូលំទូលាយនៃទិន្នន័យខ្ពស់និងតម្រូវការដែលមានការផ្លាស់ប្តូរយ៉ាងឆាប់រហ័សគឺជាវិធីសាស្រ្តធម្មតាសម្រាប់ឧបករណ៍បញ្ចូលគ្នានៃការបញ្ចូលគ្នានៃការបញ្ចូលគ្នានៃការបញ្ចូលគ្នានៃការបញ្ចូលគ្នានៃការបញ្ចូលគ្នានៃការបញ្ចូលគ្នានៃការបញ្ចូល?
សៀវភៅនេះគឺអំពីការផ្លាស់ប្តូរគំនិតរបស់ខ្ញុំនៅលើនេះ។
2. លេខកូដតែមួយ
ហេតុអ្វីបានជាយើងជឿថាវាជាយ៉ាងខ្លាំងណាស់?
លេខកូដតែមួយ
មុនពេលដែលខ្ញុំនៅក្នុងការលំបាក, សូមអរគុណនិងយល់ដឹងអំពីរបៀបដែលឧទាហរណ៍តែមួយគត់មានប្រជាប្រិយភាពដូច្នេះ។ ពួកគេមានចំណុចមួយចំនួនដែលគួរឱ្យចាប់អារម្មណ៍ដូច្នេះ:
- ការធានាចុងក្រោយសម្រាប់ការទិន្នន័យទិន្នន័យ: ទិន្នន័យចុងក្រោយដើម្បីកាត់បន្ថយទិន្នន័យចុងក្រោយ។
- ការអនុវត្តងាយស្រួល: សៀវភៅមួយចំនួននៃ SQL នៅពេលដែលអ្នកបង្កើតវេទិកាឬបន្ថែម DDL បន្ទាប់មកហើយអ្នកបានបញ្ចប់។
- គំនូរជាឯកសារ: វាត្រូវបានកំណត់នៅក្នុងគំនូរ; គំនូរនេះមិនអាចមានការចែកចាយ។
- ការបង្កើនប្រសិទ្ធិភាពនៃការសួរ: ដោយសារតែវាគឺជាឧបករណ៍អេឡិចត្រូនិការសួរនៅលើគោលបំណងនេះអាចមានល្បឿនជាងមុន។
លក្ខណៈពិសេសទាំងនេះគឺជាការល្អប្រសើរណាស់សម្រាប់គម្រោងតូចឬនៅពេលដែលទំហំទិន្នន័យអាចគ្រប់គ្រងបានហើយប្រយ័ត្នអាជីវកម្មមិនមានភាពងាយស្រួលណាស់។ ប៉ុន្តែអ្វីបានផ្លាស់ប្តូរយ៉ាងខ្លាំងនៅពេលដែលអ្នកចូលទៅក្នុង "កន្លែងសាកល្បង" នៃទិន្នន័យធំនិង iteration លឿន។
3. លេខកូដតែមួយ
នៅក្រោមអេឡិចត្រូនិ "បច្ចេកវិទ្យាខ្ពស់": តើអត្ថប្រយោជន៍ទាំងនេះក៏មានប្រសិទ្ធិភាពទេ?
លេខកូដតែមួយ
សូមពិនិត្យមើលទាំងអស់នៃ "អត្ថប្រយោជន៍" ដែលបាននិយាយនៅលើនេះហើយមើលឃើញថាតើពួកគេអាចជឿទុកចិត្តនៅក្នុងបរិស្ថានបច្ចេកវិទ្យាធំទូលំទូលាយនិងល្បឿនលឿន។
-
"The ultimate safeguard"? Is this safeguard reliable? What exactly is it safeguarding against?
It doesn't fully recognize business-level "duplicates"! Except the email case sensitivity issue I mentioned earlier (which could be solved by using
collation
but introduce more complexity in the DB layer), or phone numbers with or without+44
, or usernames with or without special characters stripped... these nuances, which business logic considers "the same," are beyond the grasp of a database's simplistic "byte-for-byte identical" unique index. It can't prevent "logical duplicates" at the business layer.The application layer has to do the heavy lifting anyway. Since all these complex "sameness" checks must be handled in the application code (you can't just throw raw database errors at users, can you?), the application layer is the true workhorse ensuring "business data uniqueness." The database's unique index is, at best, an "auxiliary police officer" whose standards might not even align with the business rules.
In distributed systems, it's merely a "local bodyguard." Once you shard your tables in a distributed scenario, an in-table unique index can't ensure global uniqueness. Global uniqueness then relies on ID generation services or application-level global validation. At this point, the "safeguard" provided by the local database index becomes even less significant.
This "ultimate safeguard" might miss the mark, has limited coverage, and relying solely on it is a bit precarious.
-
"Easy to implement"? One-time setup, week-long headache.
Adding a unique index to a brand new table is indeed just one SQL statement. But more often, you're changing the rules for an old table that's been running for ages and has accumulated mountains of data. Trying to alter a unique index on a table with tens of millions of rows (e.g., changing from a single-field unique to a composite unique) could mean several minutes of table locking! Online DDL tools might save you from service downtime, but the entire process can still be lengthy, resource-intensive, and risky.
Agile? Not so fast! In scenarios with rapid iteration, multi-region synchronization, and compliance requirements, a single unique index change at the database level can hold you up for days. So much for agility.
So, that initial "simplicity" is like bait compared to the "hell" of modifying it later.
-
"Schema as documentation"? The documentation might not match reality!
Yes, a unique index in the table structure acts as a form of "technical documentation." But "documentation" can be misleading. If the "uniqueness" defined by this index doesn't align with the actual, more complex business rules (like the case-insensitivity example), then this "documentation" is not only useless but can also mislead future developers. If changing this "documentation" (i.e., modifying the unique index) involves an epic struggle, why not write down the business rules properly in actual design documents, wikis, or code comments? Those are far easier to update.
-
"A potential query performance boost"? Is the tail wagging the dog?
This is a common misconception, or rather, an overemphasized "added value." If you simply want to speed up queries on a specific field or set of fields, you can absolutely create a regular, non-unique index for them! A non-unique index will boost query speeds just fine, and it comes without the write overhead, DDL pains, and rigid business logic constraints of a unique index.
-
Master-slave index inconsistency can instantly "paralyze" replication:
I've seen it happen multiple times: the unique index configuration on the primary database is updated (e.g., a field is added, or a constraint is changed), but the index on the replica isn't modified in sync. Then, as soon as data changes on the primary (e.g., a row is inserted that would be considered a duplicate on the replica, or the primary can write it but the replica can't due to the incorrect/outdated index), the binlog is applied to the replica, and bam!
Slave_SQL_Running: No
. Replication just dies. When this happens, you get data lag, read-write splitting is affected, and it can even impact failover capabilities. What a nightmare, right?
សូមអនុញ្ញាតឱ្យកម្រងកម្មវិធីធ្វើការងារ - វាគឺជាអ្វីដែលវាបានល្អនៅក្នុង!
ទោះបីជាបញ្ហាទាំងអស់នេះជាមួយនឹងឧបករណ៍ដំណោះស្រាយតែមួយគត់នៃទិន្នន័យទិន្នន័យ, សមត្ថភាពដើម្បីធានាឱ្យទិន្នន័យតែមួយគត់គួរតែជាលើកដំបូងទៅនឹងកម្រិតកម្មវិធីរបស់យើង។
The benefits of handling uniqueness at the application layer are numerous:
- អំណោយនិងត្រឹមត្រូវ: ប្រសិនបើអ្វីដែលអាជីវកម្មបានកំណត់ថាជាការចែកចាយ, យើងអាចកូដ logic ដូចគ្នានេះ—ប្រសិនបើប្រសិនបើមានភាពឆ្លងកាត់ប្រសិនបើមានកំណត់ប្រសិនបើមានកំណត់ប្រសិនបើមានកំណត់ប្រសិនបើមានកំណត់ប្រសិនបើមានកំណត់ប្រហែល។
- ការប្រើប្រាស់ល្អប្រសើរជាងមុន: ប្រសិនបើអ្នកប្រើប្រាស់បានធ្វើគោលបំណង, យើងអាចផ្តល់នូវការឆ្លើយតបយ៉ាងច្បាស់និងមានប្រសិទ្ធិភាពដូចជា "លេខទូរស័ព្ទនេះត្រូវបានចុះឈ្មោះមុនពេលនេះ។ អ្នកចង់ចូលទៅក្នុងការផ្លាស់ប្តូរទេ? "មិនមែនជាគោលបំណងទិន្នន័យទិន្នន័យដែលមានគោលបំណងទិន្នន័យ។
- ការឆ្លងកាត់ដំបូងដែលមានប្រសិទ្ធិភាព: ការឆ្លងកាត់បន្ថយការឆ្លងកាត់បន្ថយការឆ្លងកាត់បន្ថយការឆ្លងកាត់បន្ថយការឆ្លងកាត់បន្ថយការឆ្លងកាត់បន្ថយការឆ្លងកាត់បន្ថយការឆ្លងកាត់បន្ថយការឆ្លងកាត់បន្ថយការឆ្លងកាត់បន្ថយការឆ្លងកាត់បន្ថយការឆ្លងកាត់បន្ថយការឆ្លងកាត់។
- Interface Idempotency: វាគឺជាឧបករណ៍ដែលមានភាពងាយស្រួលប្រឆាំងនឹងប្រតិបត្តិការ duplicate ។ ប្រសិនបើអ្នកប្រើប្រាស់ចុចប៊ូតុងបញ្ចូលឬបញ្ហានៅលើបណ្តាញដែលធ្វើឱ្យមានបញ្ហានេះធ្វើឱ្យប្រសើរឡើង, idempotency ដែលមានភាពងាយស្រួលនៅលើកម្រិតកម្មវិធីធ្វើឱ្យប្រសិនបើទិន្នន័យមិនត្រូវបាន duplicate ។ ឧបករណ៍អេក្រង់តែមួយគត់មិនអាចជួយក្នុងការនេះ។
លទ្ធផល
ការប្រើប្រាស់ឧបករណ៍បញ្ជាតែមួយគត់នៅពេលដែលអត្ថប្រយោជន៍របស់វា (ជាទូទៅជាឧបករណ៍បញ្ជាទិន្នន័យចុងក្រោយជាធម្មតានៅក្នុងករណីធំបំផុត) មានប្រសិទ្ធិភាពច្រើនជាងការបញ្ហានៅក្នុងបរិស្ថានដែលមានទំហំទូលាយនៃទិន្នន័យនិងការបញ្ជាទិន្នន័យយ៉ាងឆាប់រហ័ស (ការជួសជុលយ៉ាងឆាប់រហ័ស, ការធ្វើដំណើរការ) ។ គោលបំណងសម្រាប់ឧបករណ៍បញ្ជាទិន្នន័យទិន្នន័យចុងក្រោយ (ការបញ្ជាទិន្នន័យចុងក្រោយជាធម្មតានៅក្នុងករណីធម្មតានៅក្នុងករណីធម្មតានៅក្នុងករណីធម្មតានៅក្នុងករណីធម្ម