This is probably too late for author, but for anyone with the same goal - as mentioned by @dkastl, it can be wise to use 'combinations' table, which is basically a predefined table with exact combinations of origin and destination points (vertices) for your goals.
So instead of running cost matrix on all origins to all destinations, which pgr_dijkstraCost will originally do:
origin |
dest |
1 |
10 |
2 |
20 |
3 |
30 |
4 |
40 |
in this case meaning origin 1 -> destinations 10,20,30, origin 2 -> 10,20,30 etc, resulting in 1->10, 1->20, 1->30 etc,
with 'combinations' table you have predefined ODs, meaning the same table will result in:
1->10, 2->20, 3->30, 4->40, saving you loads of time especially on your scale of calculations.
Now, to craft the combinations table that will speed things up I suggest you can run a ST_DWithin from all of your start points, in certain radius that will make sense in your case - 100m, 1000m, even more maybe. This way you will get a table where from each start point you have a number of vertices that are potentially closest to your origin vertice.
The code for looking at vertices within 100m from origins will be something like this:
CREATE TABLE tablename AS (
SELECT v.node_id as source, vc.node_id as target
FROM network_name_vertices_pgr v
JOIN network_name_vertices_pgr vc
ON ST_DWithin(v.geometry, vc.geometry, 100)
ST_DWithin uses spatial indexes that you added to your network, so it should be very fast.
In this way, instead of calculating pgr_dijkstraCost on 0.4mio x 0.4mio matrix - which is essentially 160bn OD rows, you will calculate on 0.4 mio x 10-20 closest per each, which is somewhere between 4 and 8 mio OD rows.
I imagine the speed boost will be quite big.