Optimize a Python script that calculates pairwise distances between millions of data points so that it runs within strict memory constraints.