A way to "solve" this might be (and it'd be incredibly hard) to give a constitutional right for "uneven exercise of the law" or something of that sort to be a defence against most charges: If you could demonstrate that cases are selectively investigated or prosecuted other than by clear, published standards (e.g. based on amount of available evidence), the law in question should be considered null and void until the legislative has affirmed it again.
It'd be difficult to get something like that just right so that the bar is high enough (no chance of getting laws stricken down just because police is under-unded and has to prioritise and are trying their best) but still low enough to prevent abuse, and it'd put a burden of collecting a lot of statistics and do a lot of analysis on police to ensure they don't have undue biases, but it's work they should be doing anyway.
There'd still be the option of the legislative to intentionally pass the laws again, scold the police and do nothing to actually change things, but if the bar is low enough and it happens with a lot of laws, they'd be swamped...
Another thing I'd like to see is a legal requirement that laws comes with a set of pre- and post-conditions that the law is based on. E.g. "Given that it's proven that X causes Y effect, we pass this law" and "The intent of this law is to achieve a X reduction in Y behaviour within Z timerame", and give courts the power to suspend laws where the conditions provably don't hold...
Requiring laws to be regularly re-affirmed would be another fun thing - forcing people to go on record voting for a law that today sounds antiquated and that their opponents would use to club them over the head with in the press might substantially increase the chances of having bad laws stricken down... Even "just" requiring laws to be re-affirmed if X years have gone with less than Y prosecutions would be a big deal.
A lot of problems are down to legislatives being able to pass laws and then largely wash their hands of the whole thing and not worry about the consequences or whether or not the laws have the desired effect as long as it doesn't affect the people voting for them too much. Unfortunately that's also exactly the reason we're unlikely to see it happen - it's not in their interest to have to consider the fallout.