Search code examples
mysqlperltracedbi

How can I prevent statements from being output multiple times when SQL tracing is enabled with DBI?


After connecting to a MySQL database using DBI I enable trace like this

$dbh->trace('SQL|DBD');

Then after this my SQL queries are printed twice:

>count_params statement SELECT table1.ID ID, table1.LinkID LinkID, Link.ID Link_ID FROM `table1` `table1` JOIN `table2` `Link` ON `table1`.`LinkID` = `Link`.`ID` WHERE table1.ID = ?
>parse_params statement SELECT table1.ID ID, table1.LinkID LinkID, Link.ID Link_ID FROM `table1` `table1` JOIN `table2` `Link` ON `table1`.`LinkID` = `Link`.`ID` WHERE table1.ID = ?

How can I print every executed SQL statement only once?


Solution

  • How can I print every executed SQL statement only once?

    You can't. count_params and parse_params are internal functions used by DBD::mysql to emulate prepared statements on the client. Both are called when you execute a prepared statement.

    count_params is called when a statement is prepared and contains the following:

    if (DBIc_DBISTATE(imp_xxh)->debug >= 2)
      PerlIO_printf(DBIc_LOGPIO(imp_xxh), ">count_params statement %s\n", statement);
    

    parse_params is called when a statement is executed and contains the following:

    if (DBIc_DBISTATE(imp_xxh)->debug >= 2)
      PerlIO_printf(DBIc_LOGPIO(imp_xxh), ">parse_params statement %s\n", statement);
    

    The logic is the same for both, so it's either all or nothing.