Triggers and functions, improved queries