Question

I don't have much experience with SQL databases and MariaDB, but for my understanding of how indexes work, I think there should be a way to index the first N characters of a SQL column.

Take as example the following strings in a column:

123abc
123def
123ghi
234fhg
234kgf
567kds
567ops

If I index just the first 3 characters, which are repetitive, I could get some performance gain during the SELECT operations because it would be split in 3 big blocks (123, 234, 567).

Is it possible? Or I'd have to add a second column with just the first 3 characters of each row and index it?

Was it helpful?

Solution

Yes, it is possible. Assuming your table name is tmp and the column you want to index is col, this would be the way to create an index on the first 3 characters of the column:

alter table tmp add index first3Characters (col(3));

See also: MySQL Index on first part of string

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top