XQuery选择查询无法正常工作

In our project we have large no. data (its a property listing site) and I'm storing that data to Barkeley DB (XML DB). The problem is when I am searching for a property it will list the first 10 property quickly(100% speed). Then I'm going to 2dn, 3rd page its working in the same speed. But if I'm going to 10th(30% speed) or 100th or 1500th(15%speed) page is working very slowly.

Following are my query:

let $property_ids:= 
(
    for $property in collection('bdb/properties.dbxml')/properties/property
        [  ( sale_price >=60000 and sale_price <=500000 )  and  ( building_square_footage >=300 and building_square_footage <=3000 )  and  ( bedrooms >=2 and bedrooms <=6 )  and (mls_agent_id = '505199')  ] 
    order by $property/sale_price/number() descending
    return $property/@property_id,

    for $property in collection('bdb/properties.dbxml')/properties/property
        [  ( sale_price >=60000 and sale_price <=500000 )  and  ( building_square_footage >=300 and building_square_footage <=3000 )  and  ( bedrooms >=2 and bedrooms <=6 )  and (starts-with(mls_office_id, 'CBRR') and not(mls_agent_id = '505199'))  ]
    order by $property/sale_price/number() descending
    return $property/@property_id,

    for $property in collection('bdb/properties.dbxml')/properties/property
        [  ( sale_price >=60000 and sale_price <=500000 )  and  ( building_square_footage >=300 and building_square_footage <=3000 )  and  ( bedrooms >=2 and bedrooms <=6 )  and not(starts-with(mls_office_id, 'CBRR'))  ]
    order by $property/sale_price/number() descending
    return $property/@property_id
)
return <properties>{
    for $id in subsequence($property_ids, 1, 10) return 
        collection('bdb/properties.dbxml')/properties/property[@property_id = $id]
}</properties>

And some times query will change like the following way based on the filter option in my page(means sort by only sale_price field):

 let $property_ids:= 
    (
        for $property in collection('bdb/properties.dbxml')/properties/property
        order by $property/sale_price/number() descending
        return $property/@property_id
    )
    return <properties>{
        for $id in subsequence($property_ids, 1, 10) return 
            collection('bdb/properties.dbxml')/properties/property[@property_id = $id]
    }</properties>

then from the first page its self its performance is very slow(15%).

Could you please check my query and help me to solve the issue...

Thank you, Vijesh

You're not giving the query planner enough of a chance to optimise your query.

Instead of loading a whole set of ids up and then using a subsequence of them to request a few elements, try using the FLWOR expression to retrieve the elements and get a subsequence of the elements directly.

I would also do a lot of thinking about how it could be squashed into a single FLWOR instead of three concatenated result sets before doing the subsequence too if you want the query planner to help you. I see no reason why this isn't possible given your filtering needs.