标题 | mysql使用临时表加速查询的方法 |
范文 | 本文实例讲述了mysql使用临时表加速查询的方法。分享给大家供大家参考。具体分析如下: 使用mysql临时表,有时是可以加速查询的,下面就为您详细介绍使用mysql临时表加速查询的方法。 把表的一个子集进行排序并创建mysql临时表,有时能加速查询。它有助于避免多重排序操作,而且在其他方面还能简化优化器的工作。例如: 复制代码 代码如下:select cust.name,rcvbles.balance,……other columns select cust.name,rcvbles.balance,……other columns from cust,rcvbles where cust.customer_id = rcvlbes.customer_id and rcvblls.balance>0 and cust.postcode>98000 order by cust.name 如果这个查询要被执行多次而不止一次,可以把所有未付款的客户找出来放在一个临时文件中,并按客户的名字进行排序: 复制代码 代码如下:select cust.name,rcvbles.balance,……other columns select cust.name,rcvbles.balance,……other columns from cust,rcvbles where cust.customer_id = rcvlbes.customer_id and rcvblls.balance>0 order by cust.name into temp cust_with_balance 然后以下面的方式在临时表中查询: 复制代码 代码如下:select * from cust_with_balance where postcode>98000 临时表中的行要比主表中的行少,而且物理顺序就是所要求的顺序,减少了磁盘i/o,所以查询工作量可以得到大幅减少。 注意:临时表创建后不会反映主表的修改。在主表中数据频繁修改的情况下,注意不要丢失数据。 希望本文所述对大家的mysql数据库程序设计有所帮助。 |
随便看 |
|
在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。