[mythtv] Slow MythMusic (and likely other slow list building) problem solved

Ed Benckert ed at ebonmists.com
Tue Mar 2 12:39:46 EST 2004

After much tracking through code, I found out why MythMusic is so goddamned 
slow to get past the "Please Wait..." screen when you try to play music.

When you enter that screen, it builds your list of songs, then assigns them 
numbers to sort by... normal sorting is just the order they appear, random 
sorting is just totally random, and INTELLIGENT SORTING sorts them based on 
some algorithm.

Well, if ya'll remember, in the MythMusic setup screen you can set those 
numbers that the intelligent sorting uses. Those numbers are stored in your 
MySQL database.

For some reason, when building your tree/list, Myth asks the database what 
those numbers are FOR EVERY SONG IN THE LIST.

The reason why it takes me so long to start up at home is my MySQL server is 
on a different machine that my Myth frontend... so for each of my 1800 
songs, every time I enter the play music screen, it asks the MySQL server:
What's the # for the weight for the song's Rating?
What's the # for the weight for the song's Play Count?
What's the # for the weight for the song's Last Play?
What's the # for the weight for random sorting?

So for me, thats 7200 SEPERATE MySQL select statements JUST to show me my 
play list. I didnt look at the code enough to see if the connection to MySQL 
is held open by MythTv... if it's trying to be "clean" and close the 
connection after every call, it's opening a connection, issuing a select, 
then closing the connection 7200 times for 1800 songs. Someone just posted 
on the users list that they have 40gb of songs... my 1800 is 6gb, so a rough 
estimate is he has about 12,000 songs. Thats 48,000 select statements, and 
since he's on an Xbox, chances are good his MySQL is local... if it were on 
another machine it'd take a month to get the playlist up.

The core problem here is those 4 paramaters need to be loaded ONLY ONCE when 
MythMusic starts... you cannot even edit those numbers from within 
MythMusic, you need to quit out and go to Settings->Music to even change 
them. So instead of loading them once when it starts, it loads them once for 
every song you have EVERY time you go into the play music screen, or go back 
to it from the 'Edit Playlist' option from the Play Music screen.

So, how do you fix it?

Well, the first issue is that it doesnt need to ask every time. If you're a 
code techie, go into metadata.cpp in the mythmusic source, and change how 
the GetNumSettings are loaded. I'll be doing that in a minute and will have 
a patch available shortly. What's the procedure here for submitting a patch?

The second issue is that GetSettings is broken. It asks MySQL EVERY time 
it's called. Thats totally unnecessary. I'm positive it likely slows the 
shit out of MythTv in every module that uses it... if MythMusic calls it 
unnecessarily 7200 times, then I'm sure MythTv does it somehow also. How it 
SHOULD work is there should be an internal array/tree/whatever kept in 
memory with all the settings in it. When SaveSetting is called to update the 
setting in the DB, that setting in the array should be cleared out. When 
GetSetting is called again, if the entry in the array is empty, get it from 
the database, otherwise, just return whats in the array. That I'll work on 
later though...

Anyway, thats the problem and the fix. I'll have a patch shortly... just 
need to know how to submit it and where...

More information about the mythtv-dev mailing list