I just ran into an issue where I thought a query that was being dynamically 
built was hanging. After checking the DB for the query, i don't see any of 
the where clauses in the one that was hung. What I saw was:
`SELECT * FROM table_name LIMIT 1`
So then I saw this conditional that had me wondering...
`if query.columns.include?(x)`
so I looked at the docs and saw that the columns function does actually run 
the query that was being hung. It never actually reached my dynamic query's 
execution point. 
>From previous experience in postgresql, I found that in largely populated 
tables, when running a query with a `LIMIT` clause and no `ORDER BY`, there 
are big performance implications. So even if I don't know the potential 
columns, or I don't really care about the order, I will still add an ORDER 
clause.
`SELECT * FROM my_table ORDER BY RANDOM() LIMIT 1`

I was wondering if anyone had thoughts on this or had any similar issues. 
For now I am simply doing a workaround by adding a `column_names()` method 
via a plugin to all my models.
```
def column_names
    table = self.table_name
    columns = DB.fetch("SELECT column_name from information_schema.columns 
where table_schema = 'my_schema' and table_name = '#{table}'").all
    columns.map { |i| i[:column_name].to_sym }
end
```

-- 
Confidentiality Note: The information transmitted, including attachments, 
is intended only for the person(s) or entity to which it is addressed and 
may contain confidential and/or privileged material. Any use of this 
information by persons or entities other than the intended recipient is 
prohibited. If you received this in error, please contact the sender and 
destroy any copies of this information.

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/cf862818-c63d-44f3-b94b-e81c14dac4e3n%40googlegroups.com.

Reply via email to