postgresql7.1
select * from
(select category_id,
(select count(*)
from ec_subcategories s
where s.category_id = m.category_id) subcount,
(select count(*)
from ec_subsubcategories ss
where ss.subcategory_id = m.category_id) subsubcount
from ec_category_product_map m
where product_id = :product_id
order by subcount, subsubcount, category_id)
limit 1