Java ng
如果您向我们展示了代码的其余部分,其中循环调用查询300次,我们可能会进一步提供帮助Java ng,java,jdbc,mysql,performance,Java,Jdbc,Mysql,Performance,如果您向我们展示了代码的其余部分,其中循环调用查询300次,我们可能会进一步提供帮助 一般来说,如果你每次都在计算最短路径,那么你应该建立一个类似网格的表格,为每个站点预先计算路线距离,甚至从每个站点预先计算整个路线。谢谢你的回答。是的,我有一个以站点为节点的图表。我已经用我正在使用的方法的完整代码更新了这个问题。getNeights()方法通过PriorityQueue传递给具有最低值((到达节点的成本)+(到目标节点的距离))的节点。这就是为什么每次数据库是优先级队列顶部的新节点时,我都必须
一般来说,如果你每次都在计算最短路径,那么你应该建立一个类似网格的表格,为每个站点预先计算路线距离,甚至从每个站点预先计算整个路线。谢谢你的回答。是的,我有一个以站点为节点的图表。我已经用我正在使用的方法的完整代码更新了这个问题。getNeights()方法通过PriorityQueue传递给具有最低值((到达节点的成本)+(到目标节点的距离))的节点。这就是为什么每次数据库是优先级队列顶部的新节点时,我都必须查询数据库的原因。我无法预测下一个节点将是优先级队列中的第一个节点,直到我访问相邻节点。我无法缓存停止连接数据,因为它包含10000多个连接。有什么建议吗?不要使用DB。将所有数据加载到主内存中的“图形”对象。在我的项目中,我有数百万个节点(甚至更多的边)在图中,还有一些Dijkstra算法在所有这些节点上运行,我得到的运行时间远远少于一秒钟。这个JDBC代码正在泄漏资源。尽快修复。感谢我将所有连接数据缓存在一个哈希表中,这防止了我继续连接到数据库
private HashMap<Coordinate,Node> closedNodes;
private PriorityQueue<Node> openNodes;
..
private List<Coordinate> calculatePath()
{
//While there are nodes in the open list
while (!openNodes.isEmpty())
{
//Get the node with the lowest gVal+hVal
Node node = openNodes.poll();
//Add it to the closed list
closedNodes.put(node);
//If it is not the goal node
if (!node.equals(goal))
{
//Get all the neighbours and Create neighbour node
List<Node> neighbours = helper.getNeighbours(node, goal);
//For each neighbour
for (Node neighbourNode : neighbours)
{
//Check if the neighbour is in the list of open nodes
boolean isInOpen = checkOpenNodes(neighbourNode);
//If it is not in the open nodes and not in the closed nodes
if ((!closedNodes.containsKey(neighbourNode))&& (!isInOpen))
{
//Add it to the list of open nodes
openNodes.add(neighbourNode);
}
}
}
else
{
// We found the path
path = backTrackPath(node);
break;
}
}
return path;
/**
* Gets the list of valid Nodes that are possible to travel to from <b>Node</b>
* @param stopNode Node to find neighbours for
* @param goal End Node
* @return list of neighbour Nodes
*/
public ArrayList<Node> getNeighbours(Node stopNode, Node goal)
{
ArrayList<Node> neighbours = new ArrayList<Node>();
Node neighbourNode;
//get neighbours connected to stop
try {
ResultSet rs = stmt.executeQuery("select To_Station_id, To_Station_routeID, To_Station_stopID," +
"To_Station_lat, To_Station_lng, Time from connections where Connections.From_Station_stopID ="
+stopNode.getCoord().getStopID()+" ORDER BY Connections.Time");
rs = stmt.getResultSet();
while (rs.next()) {
int id = rs.getInt("To_Station_id");
String routeID = rs.getString("To_Station_routeID");
String stopID = rs.getString("To_Station_stopID");
String stopName = rs.getString("To_Station_stopName");
Double lat = rs.getDouble("To_Station_lat");
Double lng = rs.getDouble("To_Station_lng");
int time = rs.getInt("Time");
neighbourNode = new Node(id, routeID, stopID, stopName, lat, lng);
neighbourNode.prev = stopNode;
neighbourNode.gVal = stopNode.gVal + time;
neighbourNode.hVal = heuristic.calculateHeuristic(neighbourNode, goal);
neighbours.add(neighbourNode);
}
}
catch (SQLException e) {
e.printStackTrace();
}
return neighbours;
}