I know that a deterministic ORDER BY
is when you sort by a UNIQUE INDEX
column, 1) is this true?.
I want to sort by the columns name
and percentage
.
There is a possibility that 2 or more students have the same name
and the same percentage
, therefore, the ORDER BY
in that case would be non-deterministic because MySQL
would define which one to put first and which one to put after (not me), 2) is this true?
So, my third question is (the main one), if I want to sort by several columns but still have a deterministic order, should I use a UNIQUE INDEX
column at the end of all my columns, e.g.
ORDER BY name, percentage, id
?
Considering that the id
is the primary key
I still don’t know if I understand deterministic and non-deterministic correctly.
2
Answers
ORDER BY
columns, the order will be deterministic.ORDER BY
makes it deterministic.Whether this matters depends on your application.
It can be important if you’re doing query-based replication and you use
INSERT INTO table SELECT ...
. Without deterministic ordering, you may get inconsistentAUTO_INCREMENT
IDs between the master and replicas.To answer your questions: