douwo1517 2013-11-25 14:38
浏览 75
已采纳

是否可以在PHP PDO准备语句中设置innodb_stats_on_metadata?

I transfer part of MySQL database from one server to another using XML. In this XML I have all DB related information I need (data & metadata). Everything works fine except one thing – obtaining foreign key constraints from information_schema.

The problem lies in performance. Getting approximately 100 constraints takes up about 7 minutes of PHP script loading! However, if one runs command

SET GLOBAL innodb_stats_on_metadata = 0

with constraint query (i.e. in PHPMyAdmin) like this

SET GLOBAL innodb_stats_on_metadata = 0;
SELECT * FROM information_schema.KEY_COLUMN_USAGE

everything takes up about 5 seconds and it's done. Now, this is a huge timesaver and it would be really great if I could use this in following fashion:

         try{
                $conn = $this->db->_pdo;
                $conn->setAttribute(PDO::ATTR_AUTOCOMMIT, 0); //disable autocommit for transaction consistency
                $conn->beginTransaction();

                $this->db->_pdo->exec('SET GLOBAL innodb_stats_on_metadata = 0');

                $query = $this->db->_pdo->prepare('
                    SELECT * 
                    FROM information_schema.KEY_COLUMN_USAGE 
                    WHERE REFERENCED_TABLE_NAME = ?');
                $query->bindParam(1, $table, PDO::PARAM_STR);
                $query->execute();
                $keys = $query->fetchAll(PDO::FETCH_ASSOC);

                $conn->commit();
                $conn->setAttribute(PDO::ATTR_AUTOCOMMIT, 1);

                if(!empty($keys)) return $keys; else return false;
        }catch (Exception $e){
                $conn->rollback();
                echo 'Caught exception: ',  $e->getMessage(), "
";
                return false;
        }

The problem is that exec command is just ignored here and process time is still very long. If I put that command into prepare (before SELECT and separated by semicolon) everything just ends with an error.

So my question is: How can I set mysqli DB parameter innodb_stats_on_metadata inside PHP PDO structure which uses transactions and prepare statements?

  • 写回答

1条回答 默认 最新

  • duan33360 2013-11-25 15:11
    关注

    Remove word GLOBAL from the statement. It surely won't let to set GLOBAL state variable for user without root privileges.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)