Tags with hundreds of tag ids

In my sqlth_te table I have one tag with 767 tag ids and 46 with 450+ tag ids. The remaining tens of thousands of tags have less than 10.

This is causing some tag history queries to take 5-10 minutes to run and causing DB connections to run out etc.

What steps can be taken to consolidate these tag ids?

I’m mostly wondering what causes tags to be “retired” and new ids created. It would also be great if there was a way to consolidate these.

Are you regularly reconfiguring these tags? Via scripting, perhaps?

A little bit, certainly not 700 times. We have around 100,000 tags on this gateway. Only these ~40 ish tags are very high. The remaining 90,060~ have 40 or less tag ids with 90%+ under 10 tag ids.