如何确定要在SQL查询中索引的列

时间:2018-01-14 21:35:08

标签: sql sql-server vb.net sql-server-2008 indexing

我有一个用VB.NET编写的带有SQL Server数据库的程序来管理我的电机引擎系统,到目前为止我在我的数据库中有大约15000条记录,因为我正在填写越来越多的数据,我的查询开始以指数方式放缓。

我决定通过索引来解决这个问题,但是我不知道如何确定在下面的多表列查询中索引哪些列(大约需要16秒):

SELECT 
    r.ID as [المعرّف],
    ch.ID as [معرّف القيمة],
    r.active as [مفعّل],
    en.ename as [الموتور],
    b.location as [عنوان العلبة],
    c.clientname as [المشترك],
    p.ampere as [أمبير],
    cl.fullname as [الجابي],
    b.code as [رمز العلبة],
    ec.code as [الرمز في العلبة],
    ch.previousvalue as [القيمة السابقة],
    ch.currentvalue as [القيمة الحاليّة],
    r.insurance as [تأمين],
    ((SELECT SUM(total) 
      FROM CounterHistory coh 
      WHERE coh.regid = r.ID 
        AND (coh.cyear < 2018 OR (coh.cmonth < 1 and coh.cyear = 2018))) - 
     (SELECT ISNULL(SUM(pyy.pvalue), 0) 
      FROM CounterHistory coh, Payment pyy 
      WHERE pyy.counterhistoryid = coh.ID 
        AND coh.regid = r.ID 
        AND (coh.cyear < 2018 OR (coh.cmonth < 1 and coh.cyear = 2018)))) AS [مكسورات],
    ch.notes as [ملاحظات],
    (CAST(ar.caption as nvarchar(50)) + '-' + CAST(ch.cyear as nvarchar(50))) as [شهر],(b.code + ec.code) as [رمز مفتاح],ch.monthlyfee as [رسم اشتراك],
    (ch.currentvalue-ch.previousvalue) as [فرق عداد],
    ch.kilowattprice as [سعر الكيلو],
    (((ch.currentvalue-ch.previousvalue)*ch.kilowattprice) + roundvalue) as [مطلوب كيلو],
    total+discount as [المجموع],
    discount as [حسم],
    (SELECT IsNull(Sum(pyy.pvalue),0) FROM CounterHistory coh,Payment pyy WHERE pyy.counterhistoryid=coh.ID and coh.regid=r.ID AND coh.cmonth =1 and  coh.cyear=2018) as [مدفوع],
    (total - (SELECT IsNull(Sum(pyy.pvalue),0) FROM CounterHistory coh, Payment pyy WHERE pyy.counterhistoryid=coh.ID and coh.regid=r.ID AND coh.cmonth = 1 and coh.cyear=2018)) AS [باقي]
FROM 
    Registration r, Client c, ElectricBox b, ECounter ec,
    CounterHistory ch, Package p, Engine en, Collector cl, ArabicMonth ar 
WHERE 
    r.packageid = p.ID 
    AND ch.cmonth = ar.ID 
    AND r.counterid = ec.ID 
    AND ec.boxid = b.ID 
    AND r.clientid = c.ID 
    AND ch.regid = r.ID 
    AND b.engineid = en.ID 
    AND b.collectorid = cl.ID 
    AND ch.cmonth = 1 
    AND ch.cyear = 2018 
    AND (DatePart("yyyy", r.registrationdate) < 2018 OR (DatePart("m", r.registrationdate) <= 1 AND DatePart("yyyy", r.registrationdate) = 2018)) 
ORDER BY 
    cl.fullname, b.code, ec.code

所以如何在这样的查询中确定要索引哪些列,有没有办法优化它(如果可能的话)?!

这是我的数据库构建查询:

create table ArabicMonth (ID INT primary key, caption varchar(50) not null);
create table Client (ID INT IDENtity(1,1) primary key,clientname varchar(255) not null,clientnickname varchar(255),clientmothername varchar(255),caddress varchar(255),phone varchar(50),mobile varchar(50));
create table Collector (ID INT IDENtity(1,1) primary key,fullname varchar(255) not null,caddress varchar(255),phone varchar(50),mobile varchar(50),notes varchar(1000));
create table CounterHistory (ID INT IDENtity(1,1) primary key,cmonth int not null,cyear int not null,regid int default 0,monthlyfee int default 0,kilowattprice int default 0,previousvalue int default 0,currentvalue int default 0,roundvalue int default 0,discount int default 0,total as [monthlyfee]+([kilowattprice]*([currentvalue]-[previousvalue]))+[roundvalue]-[discount],notes varchar(1000));
create table ECounter (ID INT IDENtity(1,1) primary key,serial varchar(255) not null,code varchar(5),boxid int not null default 0,active bit default 0,notes varchar(1000));
create table ElectricBox (ID INT IDENtity(1,1) primary key,code varchar(10) not null,location varchar(255),collectorid int not null,engineid int not null,notes varchar(1000));
create table Engine (ID INT IDENtity(1,1) primary key,ename varchar(255) not null,location varchar(255),epower varchar(255),company varchar(255),contactphone varchar(255),repairparty varchar(255),notes varchar(1000));
create table EngineWorkingHours (engineid int not null,cmonth int not null,cyear int not null,workinghours int default 0,notes varchar(255), primary key(engineid,cmonth,cyear));
create table Expenditure (ID INT IDENtity(1,1) primary key,expdate date not null,title varchar(255),amount int default 0,party varchar(255),detail varchar(1000));
create table Package (ID INT IDENtity(1,1) primary key,ampere decimal not null,fee int not null,insurance int not null,kilowattprice int not null,notes varchar(1000));
create table Payment (ID INT IDENtity(1,1) primary key,counterhistoryid int not null,pdate datetime not null,pvalue int not null,notes varchar(1000),collector varchar(50));
create table Registration (ID INT IDENtity(1,1) primary key,clientid int not null,registrationdate date not null,packageid int not null,counterid int not null,active bit default 0,initialcountervalue int not null,enddate date,insurance int default 0,notes varchar(1000));
create table Notes(ID int IDENTITY(1,1) NOT NULL primary key,ExpiryDate date NOT NULL,ExpiryTime time NOT NULL,nStatus varchar(255) NULL,NoteDetail varchar(1000) NULL)

alter table EngineWorkingHours add constraint fk_EngineWorkingHours foreign key (engineid) references Engine(ID);
alter table ElectricBox add constraint fk_ElectricBoxEngine foreign key (engineid) references Engine(ID);
alter table ElectricBox add constraint fk_ElectricBoxCollector foreign key (collectorid) references Collector(ID);
alter table ECounter add constraint fk_ECounterElectricBox foreign key (boxid) references ElectricBox(ID);
alter table Registration add constraint fk_RegistrationECounter foreign key (counterid) references ECounter(ID);
alter table Registration add constraint fk_RegistrationPackage foreign key (packageid) references Package(ID);
alter table Registration add constraint fk_RegistrationClient foreign key (clientid) references Client(ID);
alter table CounterHistory add constraint fk_CounterHistoryRegistration foreign key (regid) references Registration(ID);
alter table CounterHistory add constraint fk_CounterHistoryArabicMonth foreign key (cmonth) references ArabicMonth(ID);
alter table Payment add constraint fk_PaymentCounterHistory foreign key (counterhistoryid) references CounterHistory(ID);

提前致谢!!

0 个答案:

没有答案