updated metadata lookup to use pure sql for speed