You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: migrate-from-mariadb.md
+78Lines changed: 78 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -256,6 +256,84 @@ ORDER BY
256
256
257
257
See also [Character Set and Collation](/character-set-and-collation.md).
258
258
259
+
### Index length
260
+
261
+
As the following example shows, MariaDB automatically converts an index to a prefix index and returns a warning if the index exceeds the maximum key length. Unlike MariaDB, TiDB follows the MySQL behavior: it does not perform this automatic conversion and returns an error instead. Therefore, when migrating MariaDB DDL to TiDB, if an indexed column might exceed the maximum key length supported by TiDB, you need to modify your scripts to create a prefix index explicitly.
MariaDB also has special handling for unique indexes that exceed the maximum key length. For example, in the following example, MariaDB creates a `USING HASH` unique index on a `TEXT` column. TiDB does not provide this feature.
287
+
288
+
```
289
+
MariaDB> CREATE TABLE t2 (id SERIAL PRIMARY KEY, c1 TEXT NOT NULL);
290
+
Query OK, 0 rows affected (0.015 sec)
291
+
292
+
MariaDB> ALTER TABLE t2 ADD INDEX regular_index_c1 (c1);
293
+
Query OK, 0 rows affected, 1 warning (0.034 sec)
294
+
Records: 0 Duplicates: 0 Warnings: 1
295
+
296
+
Note (Code 1071): Specified key was too long; max key length is 3072 bytes
297
+
MariaDB> ALTER TABLE t2 ADD UNIQUE INDEX unique_index_c1 (c1);
To enforce uniqueness on a long text column in TiDB, you can add a generated hash column and create a unique index on that generated hash column as follows:
315
+
316
+
```
317
+
tidb> CREATE TABLE t1 (id int PRIMARY KEY, c1 TEXT NOT NULL);
318
+
Query OK, 0 rows affected (0.102 sec)
319
+
320
+
tidb> ALTER TABLE t1 ADD COLUMN c1_hash BINARY(32) AS (UNHEX(SHA2(c1,256)));
321
+
Query OK, 0 rows affected (0.242 sec)
322
+
323
+
tidb> ALTER TABLE t1 ADD UNIQUE KEY (c1_hash);
324
+
Query OK, 0 rows affected (0.363 sec)
325
+
326
+
tidb> INSERT INTO t1(id,c1) VALUES (1,'aaa');
327
+
Query OK, 1 row affected (0.015 sec)
328
+
329
+
tidb> INSERT INTO t1(id,c1) VALUES (2,'bbb');
330
+
Query OK, 1 row affected (0.006 sec)
331
+
332
+
tidb> INSERT INTO t1(id,c1) VALUES (3,'aaa');
333
+
ERROR 1062 (23000): Duplicate entry '\x984\x87m\xCF\xB0\\xB1g\xA5\xC2IS\xEB\xA5\x8CJ\xC8\x9B\x1A\xDFW' for key 't1.c1_hash'
334
+
tidb>
335
+
```
336
+
259
337
## Dump data with Dumpling and restore data with TiDB Lightning
260
338
261
339
This method assumes that you take your application offline, migrate the data, and then re-configure your application to use the migrated data.
0 commit comments