Permalink
Browse files

Marking episode first/last showings performance

Mythfilldatabase's "Marking episode first showings" and "Marking
episode last showings" steps take a combined 21+ minutes on my backend
server. After attempting MySQL and filesystem tuning with no success,
I cut the updates down to 5 seconds by modifying mythfilldatabase's
queries to update in bulk rather than row-by-row.

Besides the performance improvement, the "Found" number is changed to
the actual number of programs marked instead of the number of unique
programs. The number marked is higher than the unique count because of
duplicate channels (non-HD and HD variants, Comcast in the middle of a
massive renumbering).

Fixes #10482

Signed-off-by: David Engel <dengel@mythtv.org>
  • Loading branch information...
1 parent 71525eb commit bb58a94fb5c1b205cab30cba5a109605a3288e1b @sfkoch sfkoch committed with gigem Apr 10, 2012
Showing with 58 additions and 78 deletions.
  1. +58 −78 mythtv/programs/mythfilldatabase/main.cpp
@@ -648,87 +648,67 @@ int main(int argc, char *argv[])
MythDB::DBError("Clearing first and last showings", updt);
LOG(VB_GENERAL, LOG_INFO, "Marking episode first showings.");
-
- MSqlQuery query(MSqlQuery::InitCon());
- query.prepare("SELECT MIN(starttime),programid FROM program "
- "WHERE programid > '' GROUP BY programid;");
- if (query.exec())
- {
- updt.prepare("UPDATE program set first = 1 "
- "WHERE starttime = :STARTTIME "
- " AND programid = :PROGRAMID;");
- while(query.next())
- {
- updt.bindValue(":STARTTIME", query.value(0).toDateTime());
- updt.bindValue(":PROGRAMID", query.value(1).toString());
- if (!updt.exec())
- MythDB::DBError("Marking first showings by id", updt);
- }
- }
- int found = query.size();
- query.prepare("SELECT MIN(starttime),title,subtitle,"
- " LEFT(description, 1024) AS partdesc "
- "FROM program WHERE programid = '' "
- "GROUP BY title,subtitle,partdesc;");
- if (query.exec())
- {
- updt.prepare("UPDATE program set first = 1 "
- "WHERE starttime = :STARTTIME "
- " AND title = :TITLE "
- " AND subtitle = :SUBTITLE "
- " AND LEFT(description, 1024) = :PARTDESC");
- while(query.next())
- {
- updt.bindValue(":STARTTIME", query.value(0).toDateTime());
- updt.bindValue(":TITLE", query.value(1).toString());
- updt.bindValue(":SUBTITLE", query.value(2).toString());
- updt.bindValue(":PARTDESC", query.value(3).toString());
- if (!updt.exec())
- MythDB::DBError("Marking first showings", updt);
- }
- }
- found += query.size();
+ updt.prepare("UPDATE program "
+ "JOIN (SELECT MIN(starttime) AS starttime, programid "
+ " FROM program "
+ " WHERE programid <> '' "
+ " GROUP BY programid "
+ " ) AS firsts "
+ "ON program.programid = firsts.programid "
+ " AND program.starttime = firsts.starttime "
+ "SET program.first=1;");
+ if (!updt.exec())
+ MythDB::DBError("Marking first showings by id", updt);
+ int found = updt.numRowsAffected();
+
+ updt.prepare("UPDATE program "
+ "JOIN (SELECT MIN(starttime) AS starttime, title, subtitle,"
+ " LEFT(description, 1024) AS partdesc "
+ " FROM program "
+ " WHERE programid = '' "
+ " GROUP BY title, subtitle, partdesc "
+ " ) AS firsts "
+ "ON program.starttime = firsts.starttime "
+ " AND program.title = firsts.title "
+ " AND program.subtitle = firsts.subtitle "
+ " AND LEFT(program.description, 1024) = firsts.partdesc "
+ "SET program.first = 1 "
+ "WHERE program.programid = '';");
+ if (!updt.exec())
+ MythDB::DBError("Marking first showings", updt);
+ found += updt.numRowsAffected();
LOG(VB_GENERAL, LOG_INFO, QString(" Found %1").arg(found));
LOG(VB_GENERAL, LOG_INFO, "Marking episode last showings.");
- query.prepare("SELECT MAX(starttime),programid FROM program "
- "WHERE programid > '' GROUP BY programid;");
- if (query.exec())
- {
- updt.prepare("UPDATE program set last = 1 "
- "WHERE starttime = :STARTTIME "
- " AND programid = :PROGRAMID;");
- while(query.next())
- {
- updt.bindValue(":STARTTIME", query.value(0).toDateTime());
- updt.bindValue(":PROGRAMID", query.value(1).toString());
- if (!updt.exec())
- MythDB::DBError("Marking last showings by id", updt);
- }
- }
- found = query.size();
- query.prepare("SELECT MAX(starttime),title,subtitle,"
- " LEFT(description, 1024) AS partdesc "
- "FROM program WHERE programid = '' "
- "GROUP BY title,subtitle,partdesc;");
- if (query.exec())
- {
- updt.prepare("UPDATE program set last = 1 "
- "WHERE starttime = :STARTTIME "
- " AND title = :TITLE "
- " AND subtitle = :SUBTITLE "
- " AND LEFT(description, 1024) = :PARTDESC");
- while(query.next())
- {
- updt.bindValue(":STARTTIME", query.value(0).toDateTime());
- updt.bindValue(":TITLE", query.value(1).toString());
- updt.bindValue(":SUBTITLE", query.value(2).toString());
- updt.bindValue(":PARTDESC", query.value(3).toString());
- if (!updt.exec())
- MythDB::DBError("Marking last showings", updt);
- }
- }
- found += query.size();
+ updt.prepare("UPDATE program "
+ "JOIN (SELECT MAX(starttime) AS starttime, programid "
+ " FROM program "
+ " WHERE programid <> '' "
+ " GROUP BY programid "
+ " ) AS lasts "
+ "ON program.programid = lasts.programid "
+ " AND program.starttime = lasts.starttime "
+ "SET program.last=1;");
+ if (!updt.exec())
+ MythDB::DBError("Marking last showings by id", updt);
+ found = updt.numRowsAffected();
+
+ updt.prepare("UPDATE program "
+ "JOIN (SELECT MAX(starttime) AS starttime, title, subtitle,"
+ " LEFT(description, 1024) AS partdesc "
+ " FROM program "
+ " WHERE programid = '' "
+ " GROUP BY title, subtitle, partdesc "
+ " ) AS lasts "
+ "ON program.starttime = lasts.starttime "
+ " AND program.title = lasts.title "
+ " AND program.subtitle = lasts.subtitle "
+ " AND LEFT(program.description, 1024) = lasts.partdesc "
+ "SET program.last = 1 "
+ "WHERE program.programid = '';");
+ if (!updt.exec())
+ MythDB::DBError("Marking last showings", updt);
+ found += updt.numRowsAffected();
LOG(VB_GENERAL, LOG_INFO, QString(" Found %1").arg(found));
}

0 comments on commit bb58a94

Please sign in to comment.