How to redirect 40,000 urls with the same url structure efficiently?

That can be solved without WordPress per .htaccess (or whatever config file your webserver is using).

Example for .htaccess:

RedirectMatch Permanent /School-(.*)-Cost /School-$1/Cost

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)