I'm working on a shortest path a* algorithm in java with a mysql db. I'm executing the following SQL Query approx 300 times in the program to find route connections from a database of 10,000 bus connections. It takes approx 6-7 seconds to execute the query 300 times. Any suggestions on how I can speed this up or any ideas on a different method i can use ? Thanks
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;
}