optimize sql and fix a aes bug