在SQL中使用now()导致的性能下降(MySQL)
做数据库调优,一个简单的tip就使性能提升一大块时,被帮助的人自然是高兴而且感激,我也是满心欢喜。用所学帮助了他人,也说明还是有一技之长的,有一门能够养活自己的手艺。也算是手艺人啊。
同事让我帮助优化,在slow query log里发现有两个SQL执行的次数最多,并且每次都在两秒以上。用explain看了,也未发现索引使用方面的问题。
最后把怀疑的目光落在了SQL结尾处的>=now()上,now()每次都需要从系统中取,因此可能会导致无法使用query cache。
了解了一下需求,实际这里并不需要当时的时刻,只需要当天的日期就行了。因此建议同事把>=now()改成>=当前日期,而当前日期在程序中取得。
果然,性能大幅提高。
相关文档:
以前在安装sql的时候,如此提示,我只要重新启动即可,可是今天重新启动了N次计算机,问题却丝毫没有解决,依然提示这样的话。“以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机。” 只好google以下,最终得知是安装程序在先前的安装过程中 ......
--从Excel文件中,导入数据到SQL数据库中,很简单,直接用下面的语句:
/*===================================================================*/
--如果接受数据导入的表已经存在
insert into 表 select * from
OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;DATABASE=c:\test.xls',sheet1$)
--如 ......
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Grids, DBGrids, DB, ADODB,comobj, OleServer,
ExcelXP;
type
TForm1 = class(TForm)
ADOConn: TADOConnection;
& ......
/*
use master
go
if DB_ID('UserImage') is not null
drop database UserImage
create database UserImage
go
use UserImage
go
create table Images
(
Image_Name nvarchar(255) primary key,
Image_Data Image not null
)
go
create proc InsertImage
(
@Image_Name nv ......