Home > database > Query whether there is order, within 90 days which high efficiency?
Query whether there is order, within 90 days which high efficiency?
Time:09-25
Good drivers, I now have a demand, within 90 days to query a merchant whether have order, I have found two implementations, one is to use the exists, one kind is with the count, don't know what kind of better, old can help analyze the driver?
By using the exists a:
SELECT 1 FROM dual WHERE the EXISTS (SELECT * FROM T_ORDER WHERE customer_no='1111111' And create_time & gt; To_date (' 2017-10-24 00:00:00 ', '- dd yyyy - mm hh24: mi: ss') And create_time & lt; 23:59:59 to_date (' 2018-01-24 ', '- dd yyyy - mm hh24: mi: ss') );
The way using count:
SELECT count (1) the from T_ORDER where customer_no='1111111' And create_time & gt; To_date (' 2017-10-24 00:00:00 ', '- dd yyyy - mm hh24: mi: ss') And create_time & lt; 23:59:59 to_date (' 2018-01-24 ', '- dd yyyy - mm hh24: mi: ss');
Below is the test data, eliminating the various indexes of what,
Drop table "T_ORDER";
CREATE TABLE "T_ORDER ( ""ID" INT the NOT NULL, "CUSTOMER_NO" VARCHAR2 (30) NULL, "CREATE_TIME" DATE NULL );
INSERT INTO "T_ORDER" (" ID ", "CUSTOMER_NO", "CREATE_TIME") VALUES (' 1 ', '1111111', TO_DATE (' 2018-01-24 11:25:02 ', 'SYYYY - MM - DD HH24: MI: SS')); INSERT INTO "T_ORDER" (" ID ", "CUSTOMER_NO", "CREATE_TIME") VALUES (' 2 ', '1111111', TO_DATE (' 2018-01-08 11:25:15 ', 'SYYYY - MM - DD HH24: MI: SS')); INSERT INTO "T_ORDER" (" ID ", "CUSTOMER_NO", "CREATE_TIME") VALUES (' 3 ', '1111111', TO_DATE (' 2018-01-01 11:25:24 ', 'SYYYY - MM - DD HH24: MI: SS'));
CodePudding user response:
Use way two, from the point of execution plan, both should be the same, is inherently involves only a single standard, it is not necessary to the entire table more the correlation method and way of this kind of writing is not recommended,
Should have on customer_no index, if there are customer_no, create_time composite index performance is better, Attention should be paid to customer_no this field value to and the field type is consistent, avoid implicit conversion to go full table,