Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6380923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:19:49+00:00 2026-05-25T02:19:49+00:00

Imagine you have a large number of records inside an XQuery-based XML database: <widgets>

  • 0

Imagine you have a large number of records inside an XQuery-based XML database:

<widgets>
   <widget id="1" name="Foo Widget" price="19.99" />
   <widget id="2" name="Bar Widget" price="29.99" />
   <widget id="3" name="Baz Widget" price="39.99" />
   <!-- etc. -->
</widget>

By a ‘large number’, I mean a million or more.

You wish to retrieve one item from the list randomly using XQuery:

let $widgets := for $widget in //widgets/widget
  order by util:random()
  return $widget

for $val in subsequence($widgets, 1, 1)
  return $val

When the number of records grows, the evaluation takes an extravagant amount of time to run as it seems to load everything from the database and reorder it in memory. I think that might be O(n log 2n). Sigh-inducing slowness.

Is there a lazier, better way of doing this?

There’s the “get a count of the number of items, then randomly pick a number from zero to count” method, which I’d rather avoid.

Ideally, the database could do it, if there were some kind of feature like:

let $widgets := for $widget in //widgets/widget
  order by util:random()
  limit 1
  return $widget

This would be FLOLWR, I guess. But it’s not in the XQuery spec, even though it’s a common enough thing one might do in SQL (or indeed SPARQL or a number of other query languages).

Is there any way of getting this? Adding a where clause would do it, but where clauses get evaluated before order clauses, which doesn’t really help.

Any suggestions? (The application sending the XQueries is written in Java, and the XML database is eXist, if that helps with any slightly more curveball, off-the-wall ideas.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:19:49+00:00Added an answer on May 25, 2026 at 2:19 am

    The optimizer might do a better job if you don’t use an intermediate variable, but that’s a big might.

    subsequence(
     for $widget in //widgets/widget
      order by util:random()
      return $widget
     ,1,1)
    

    I suspect the “method you’d rather avoid” will perform better, but the proof is in the benchmarking.

    //widgets/widget[util:random(count(//widgets/widget))]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine you have a large number of video files stored on a server, and
Question We have a large number of xml configuration files that we want merged
Imagine you have a large array of floating point numbers, of all kinds of
Imagine you have a large dataset that may or may not be filtered by
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into
Imagine I have these python lists: keys = ['name', 'age'] values = ['Monty', 42,
Say I have the following data Name Value =============== Small 10 Medium 100 Large
I have to process a large table (2.5B records) row by row in order
I still have a large number of floppies. On some of them there probably
I have in my model an object, that when modified requires a large number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.