以下是我拥有的休眠 sql 查询的原始内容:
<sql-query name="countryOfOrigin-limit-country-city-location">
<return alias="rb" class="RecentBooking"/>
SELECT
bb.reserv_num as {rb.reservNum},
bb.origin as {rb.countryOfOrigin},
bb.pick_up_loc as {rb.locationId},
bb.first_date as {rb.bookingDate},
bb.pick_up_time as {rb.pickUpDate},
bb.drop_off_time as {rb.dropOffDate},
bb.car_price as {rb.carPrice},
bb.discount as {rb.discount},
bb.exchange_rate as {rb.exchangeRate},
SUBSTRING(a.internal_class,1,1)as {rb.carClass},
a.car_type as {rb.carType},
bb.vehicle_type as {rb.vehicleType},
s.name as {rb.supplier},
vv.country as {rb.country},
vv.city as {rb.city},
vv.location as {rb.location},
bb.cur as {rb.currency},
a.pics as {rb.carImage}
FROM (SELECT * FROM b WHERE first_date > DATE(NOW()-1)AS bb
JOIN a a ON a.id = bb.car_id
JOIN d d ON d.id = bb.pick_up_loc
JOIN supplier s ON s.id = d.supplier_id
JOIN v_location_trans vv ON vv.location_id = d.location_id
AND vv.lang=33
AND vv.country = :country
AND vv.city = :city
AND vv.location = :location
AND bb.origin = :countryOfOrigin
ORDER BY bb.id DESC
LIMIT :limit
</sql-query>
sql 运行得足够快,但正如您可能已经注意到的那样,NOW()-1 应该是 NOW() - INTERVAL 1 DAY。
我修改了嵌套的select语句如下:
FROM (SELECT * FROM b WHERE first_date > DATE(NOW() - INTERVAL 1 DAY)) AS bb
该修复程序在结果方面效果很好,但是查询速度大大减慢(几乎几乎瞬间达到几秒钟)。
我相信这是因为 DATE(NOW() - INTERVAL 1 DAY) 被评估了很多次(我们有一个非常大的数据集)。我将如何分离这个计算以便它只发生一次,而不必从 Java 代码(如国家、城市、限制等)中传递它?
我对休眠或 SQL 的经验很少,我尝试在 select 语句之前将计算分离到一个变量中,这是不允许的。我还尝试对 SQL 查询进行小幅更改,但我不断收到运行时错误,可能是由于 SQL 无效。
我该怎么做呢?
谢谢。