简体   繁体   中英

How to normalize mysql database table

For a mysql database I have two similar tables:

1.st table track_tag (2 million rows)

id (int), track_id (int), tag (varchar)

2.nd table tag (150.000 rows)

id (int), tag(varchar)

I want to normalize first table tag column with the second table's primary key. What is the fastest way to solve this problem?

Assuming you're not working on a production DB (and hence things like long-running queries, and dropping/re-creating tables are OK) - then I think the fastest way would be to use a Create Table AS SELECT statement .

For example:

CREATE TABLE track_tag_V2 AS SELECT track.id AS id, track.track_id, tag.id AS tag_id
FROM track_tag track, tag
WHERE track.tag = tag.tag
;

And then (Assuming the structure is to your liking) simply DROP track_tag and then RENAME track_tag_V2 to track_tag and you're done!

However, this may not (and probably will not) the the BEST way. If your DB is de-normalised to this point, it may have been done for performance reasons already (normalisation optimises storage, not performance). It may also be in need of a total re-design (which is not fast).

Your tag table is fine with columns as : id (int), tag(varchar)

Note: Assuming id field is here primary key other wise create index on it.

But change track_tag table as per below-

id (int), track_id (int), tag_id (int)

Note: tag_id field should be indexed here.

Also not clear what is track_id field here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM