我正在查询一个包含数千行的相当大的数据库。它大约需要 5 分钟,而它应该需要大约 5 秒。
ResultSet rs = st.executeQuery("SELECT flights.airline, flights.flight_num,"
+ "origin_airport_code, destination_airport_code,"
+ "ticket.date, ticket.bought_date, ticket.price "
+ "FROM flights JOIN flightinfo JOIN ticket");
我应该使用某种特定类型的连接 JOIN 语句吗?
最佳答案
目前,您的查询所做的是获取表的笛卡尔积,因为您尚未指定它们的连接条件或定义表之间的关联方式。
您需要在每个连接语句中添加一个条件或ON
子句,例如
SELECT flights.airline,
flights.flight_num,
origin_airport_code,
destination_airport_code,
ticket.date,
ticket.bought_date,
ticket.price
FROM flights
INNER JOIN flightinfo
ON flights.colName = flightinfo.colname -- << 1
INNER JOIN ticket
ON table.ColName = ticket.colName -- << 2
地点:
1 >> Colname >> how the two tables:flights and flightinfo, are related
2 >> table >> I don't know where the table ticket will be joined, it is
either flights or flightinfo.
要进一步了解有关连接的更多信息,请访问以下链接:
关于java - 查询时间慢 SQL/Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16006714/