Analytics functions in db