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 6717091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:48:42+00:00 2026-05-26T08:48:42+00:00

I need to take random row from table. At first, i wrote SP like

  • 0

I need to take random row from table. At first, i wrote SP like this:

SELECT * FROM 
    (SELECT TOP 1 
     *
     FROM PeopleTales PT
     LEFT JOIN PeopleTalesCategories PTC ON PT.CategoryAn = PTC.Analit
     WHERE LEN(PT.Text) > 900
     ORDER BY NEWID()
    ) t1

This is not very good in Time statistics:

Average total execution time = 200.

Next, i tried to take row manually:

WITH CTE_t
AS
(
        SELECT
        ROW_NUMBER () OVER (ORDER BY PT.Analit) AS RowNumber,
        *
        FROM PeopleTales PT
        LEFT JOIN PeopleTalesCategories PTC ON PT.CategoryAn = PTC.Analit
        WHERE LEN(PT.Text) > 900
)


SELECT * FROM CTE_t
WHERE CTE_t.RowNumber = 1 + ABS(CHECKSUM(NewId())) % (SELECT COUNT(CTE_t.RowNumber) FROM CTE_t)

It’s a little better by execution time, but sometimes i had 0 rows, sometimes 1,2 or even 3 rows in result! + i know, using CTE isn’t very good. It’s not decision.

Average total execution time = 60.

So, next step:

SELECT * FROM (
    SELECT
        ROW_NUMBER () OVER (ORDER BY PT.Analit) AS RowNumber,
        *
    FROM PeopleTales PT
    LEFT JOIN PeopleTalesCategories PTC ON PT.CategoryAn = PTC.Analit
    WHERE LEN(PT.Text) > 900
    ) t1
WHERE t1.RowNumber = (SELECT 1 + ABS(CHECKSUM(NewId())) % (SELECT 
                                                            COUNT(Analit) 
                                                            FROM PeopleTales
                                                            WHERE LEN(Text) > 900))

Every time i have just one row, and its ok with total execution time.

Average total execution time = 60.

So, is it more ways to take random row or optimize my query?

Thx alot for responses.

  • 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-26T08:48:42+00:00Added an answer on May 26, 2026 at 8:48 am

    How many rows are in the table, and is it possible to modify your schema? One problem is that all the selects are based on LEN(Column), so SQL Server can’t use an index effectively.

    What you might want to consider is a computed column, and placing an index on it. Something like:

    ALTER TABLE PeopleTales ADD TextLength AS LEN(PT.Text)
    GO
    CREATE INDEX IX_PeopleTales_TextLen ON PeopleTales (Text, CategoryAN)
    GO
    

    I’m including CategoryAN so there’s the join is fulfilled purely by the index (and means SQL Server doesn’t have to do a bookmark or key lookup).

    Then, obviously, change your queries so that WHERE LEN(PT.Text) > 900 becomes WHERE TextLength > 900.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to take some row. They came from sql TARIH (sql column) is
Please take a look at this link: Random entry from dictionary That example is
Need to take a SELECT drop down list options and find if any of
I need to take a web page and extract the address information from the
I need to take a paragraph of text and extract from it a list
I already wrote a random generator which take arguments a and b, where a
I need to generate random numbers from Binomial(n, p) distribution. A Binomial(n, p) random
I want to show a random record from the database. I would like to
this is basically what I'm doing. I select a science article from en.wikipedia.org and
I need a photo album extension for Joomla that can take selected folders from

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.