问题
We are planning to create an table like below in production environment but concerning about what would happen to storage and query/insert/update performance if we create a constraint key with quite a lot of fields in MySQL like this:
create table table_name
(
f1 int null,
f2 int null,
f3 int null,
f4 int null,
f5 int null,
f6 int null,
f7 int null,
f8 int null,
f9 int null,
f10 int null,
f11 int null,
f12 int null,
f13 int null,
f14 int null,
f15 int null,
f16 int null,
f17 int null,
f18 int null,
f19 int null,
f20 int null,
v1 int null,
v2 int null,
v3 int null,
constraint update_key unique (f1, f2, f3, f4, f5, f6, f7, f8, f9, f10,
f11, f12, f13, f14, f15, f16, f17, f18, f19, f20);
And insert rows with SQL like this:
insert into table_name (f1,....f20, v1, v2, v3)
values (values ...)
on duplicate key update
v1=v1+values(v1),
v2=v2+values(v2),
v3=v3+values(v3);
Could anyone give us some advice?
回答1:
The presence of an index in your table adds some overhead to your INSERT/UPDATE/DELETE statements.
A 20-column index isn't worse than a 1-column index. Or at least, the difference in performance is small enough that you shouldn't change your design because of it.
Here's an analogy: Your mother asks you to go to the store to pick up groceries. You have to put on your shoes, take the bus to the store, collect groceries, stand in line to pay for them, take the bus home, and put the groceries in the fridge. Does it matter a lot if she asks for 1 item or 2 or 20? It's true that it's a bit heavier to carry the bag, but the time taken for the trip doesn't change.
来源:https://stackoverflow.com/questions/57739231/would-constraint-unique-key-with-lot-of-fields-hurt-mysql-performance