Fix slow query for data gaps