randomize drupal database query - sql

I'm executing a query which I want to randomize query results from a drupal database.
here's what I have done...
$res=db_query("SELECT DISTINCT nid FROM content_type_event ORDER BY RANDOM()");
but this query doesn't work. what's wrong with this query? please help me to solve this?
Thanks a lot...

You probably need to be using ORDER BY RAND() instead.
Also there is a bit you may want to know about ORDER BY RAND()'s performance if you are getting a considerable amount of results that will be randomized:
http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/
EDIT: Just saw your comment that you are using PostgreSQL, this looks like it could be helpful: http://www.petefreitag.com/item/466.cfm
Sorry I can't be a lot of much help, I don't use PostgreSQL myself

You did not tell us which database engine you are using, but maybe ORDER BY RAND() would be better.
Update: okay, PostgreSQL uses RANDOM() and not RAND() so that's okay. I've found this question which seems to suggest that ORDER BY RANDOM() should work and the error is likely to be elsewhere. Maybe it's DISTINCT that is screwing things up; try your query again without DISTINCT.

Related

Understanding random ordering in Rails + postgresql

So, I'd like to do some random ordering when displaying data the code I have at this point is:
Timsheet.limit(1).offset(RANDOM(Timesheet.count)).first
I know that postgresql's (RANDOM) syntax is different than MYSQL's (RAND()) and Oracles (dbms_random.value) syntax.
I'd just like to confirm that my code is correct, from what I understand it's doing is, it's grabbing the first row, and offsetting the data in a random order?
Please help clear this up, thanks!
I think the following will work with all DBMS's
Timsheet.offset(Random.new.rand(Timsheet.count)).first

Pagination with SQLite using LIMIT

I'm writing my own SQLIteBrowser and I have one final problem, which apparently is quite often discussed on the web, but it doesn't seem to have a good general solution.
So currently I store the SQL which the user entered. Whenever I need to fetch rows, I execute the SQL by adding "Limit n, m` at the end of the SQL.
For normal SQLs, which I mostly use, this seems good enough. However if I want to use limit myself in the query, this will obviously give an error, because the resulting sql can look like this:
select * from table limit 30 limit 1,100
which is obviously wrong. Is there some better way to do this?
My idea was, that I could scan the SQL and check if there is a limit clause already used and then ignore it. Of course it's not as simlpe as that, because if I have an sql like this:
select * from a where a.b = ( select x from z limit 1)
it obviously should still apply my limit in such a case, so I could scan the string from the end and look if there is a limit somehwere. My question now is, how feasable this is. As I don't know who the SQL parser works, I'm not sure if LIMIT has to be at the end of SQL or if there can be other commands at the end as well.
I tested it with order byand group by and I get SQL errors if limit is not at the end, so my assumption seems to be true.
I found now a much better solution which is quite simple and doesn't require me to parse the SQL.
The user can enter an arbitrary sql. The result is loaded into a table. Since we don't want to load the whole result at once, as this can return millions of records, only N records are retriueved. When the user scroll to the bottom of the table the next N items are fetched and loaded into the table.
The solution is, to wrapt the SQL into an outer sql with my page size limits.
select * from (arbitrary UserSQL) limit PageSize, CurrentOffset
I tested it with SQLs I regularly use, and this seem to work quite nicely and is also fast enough for my purpose.
However, I don't know wether SQLite has a mechanism to fetch the new rows faster, or if the sql has to be rerun every time. In that case it might not be a good solution fo rrealy complex queries with a long response time.

Operations on selected items

I want to have query like this:
SELECT
sum(data_parts.size) as size_sum,
(size_sum/2.) as half_of_size_sum
FROM
data_parts
WHERE
data_parts.some_id='1';
Of course it won't work, because there's no column named size_sum, so my question is:
Is there a way to use size_sum as a parameter in next select item?
Other than using a subquery containing your current query (see Davide's answer), I don't think there is a way to do that.
But you could always do:
SELECT
sum(data_parts.size) as size_sum,
(sum(data_parts.size)/2.) as half_of_size_sum
FROM
data_parts
WHERE
data_parts.id='1';
Postgres is smart enough to only get that sum once. Only if this query will be greatly expanded with more calculations being done on size_sum would I recommend the subquery approach. Not because it works better, but because it will be easier to read. But if the current calculation is all you need, don't bother with a subquery. It would actually be less easy to read.
yes, a (somewhat) ugly way of making the query run there is...
SELECT
SIZE_SUM,
SIZE_SUM/2 AS HALF_OF_SIZE_SUM
FROM (
SELECT
sum(data_parts.size) as size_sum)
FROM
data_parts
WHERE
data_parts.id='1') X;
But I don't think there is a way on postgre to do operations directly based on the previous select fields
No, and too unnecesary to use subselects for this, simply use SUM(data_parts.size) again.
I'm not sure whether I catch your point.
Do you want the code like this?
SELECT
sum(data_parts.size) as size_sum,
(sum(data_parts.size)/2.) as half_of_size_sum
FROM
data_parts
WHERE
data_parts.some_id='1';

How to sql-recursive statements work?

I am trying to understand sql recursive statements, but it is really hard for me. For example:
Thats an exampe I am trying to understand and to write down the output.
Can sb pls explain me how this works, step by step?
greetings and thx in advance
maya
Also, see here - http://www.postgresql.org/docs/8.4/static/queries-with.html, where the steps of the recursive query evaluation are described.
The UNION ALL portion gets recursed until it returns no more records. I don't know if you can have multiple UNION ALL portions.

Maximum values in wherein clause of mysql

Do anyone knows about how many values I can give in a where in clause? I get 25000 values in a where in clause and mysql is unable to execute. Any thoughts? Awaiting for your thoughts
Although this is old, it still shows up in search results so is worth answering.
There is no hard-coded maximum in MySQL for the length of a query. This includes all parts of the query such as the WHERE clause.
However, there is a value called max_allowed_packet which determines the largest query you can run on the MySQL server process. It isn't to do with the number of elements in the query, but the total length of the query. So
SELECT * FROM mytable WHERE mycol IN (1,2,3);
is less likely to hit the limit than
SELECT * FROM mytable WHERE mycal IN ('This string','That string','Tother string');
The value of max_allowed_packet is configurable from server to server. But almost certainly, if you find yourself hitting the limit because you're writing SQL statements of epic length (rather than dealing with binary data which is a legitimate reason to hit it), then you need to re-think your SQL.
I think that if this restriction is a problem then you're doing something wrong.
Perhaps you could store the data from your where clause in a table and then join with it. This would probably be more efficient.
I think it is something with execution time.
I think you are doing soemthing like this: Correct me if i am wrong:
Select FROM table WHERE V1='A1' AND V2='A1' AND V3='A3' AND ... Vn='An'
There is always a efficient way how you can do your SELECT in your database. Working in a database is importent to keep in your mind that seconds are very importent.
If you can share how your query is look like, then we can help you making a efficient SELECT statement.
I wish u succes