Everybody is good, I use the following SQL statement
SELECT Count (*) FROM [tblHitRecordsDtl] inner join [tblHitRecords] on [tblHitRecords]. Id=[tblHitRecordsDtl] hitRecId where [tblHitRecordsDtl]. Rank=1 and (recTime & gt;=1603419942778 and recTime & lt;= 1604419942778)
Among them, the recTime is of type Integer, When the where condition after only rank=1, under the speed of about 500 ms, but after add recTime judgment, It takes about 3-4 seconds... Data table only 50000, want to board a great god how to solve? Thank you.
CodePudding user response:
First of all, of the following does not consider your hardware problems, Slow is inner join this operation, the speed depends on what you want to join the two tables of the initial size of the, Minimize the number of connections you two tables, where judgment first before connection, can improve the speed, Parameter recTime, you did not indicate what is a table, I'll default to the value in table tblHitRecords,
SELECT Count (1) The FROM (SELECT * FROM [tblHitRecordsDtl] WHERE [tblHitRecordsDtl]. Rank=1) AS a t1 INNER JOIN (SELECT * FROM [tblHitRecords] WHERE [tblHitRecords] recTime & gt;=1603419942778 AND [tblHitRecords] recTime & lt; AS t2 ON t2=1604419942778). The id=t1. HitRecId WHERE [tblHitRecordsDtl]. Rank=1 AND (recTime & gt;=1603419942778 AND recTime & lt; =1604419942778)
Secondly if you put the t1, t2 select * change as you need a small amount of field, can also be a certain speed queries,
The reason is that the recTime the scope retrieval, itself is slower than rank=1 this,
Then you can also consider the factor of the index, the need to retrieve fields and indexes, specific methods you can check it, I no longer here,
CodePudding user response:
Sorry where forget to delete the correction of SQL is:
SELECT Count (*) The FROM (SELECT * FROM [tblHitRecordsDtl] WHERE [tblHitRecordsDtl]. Rank=1) AS a t1 INNER JOIN (SELECT * FROM [tblHitRecords] WHERE [tblHitRecords] recTime & gt;=1603419942778 AND [tblHitRecords] recTime & lt; AS t2 ON t2=1604419942778). The id=t1. HitRecId
CodePudding user response:
Very glad to receive your reply, thank you, I will be your statement of the second [tblHitRecordsDtl] instead to perform after t1, The results below... Speed is still the same, Don't know the type of recTime especially Int not DateTime "there...