sql slow postgresql dbeaver - sql

I am using DBeaver to query a PostgreSQL database.
I have this query, it simply selects the highest id per Enterprise_Nbr. The query works but is really slow. Is there any way I can rewrite the query to improve performance.
I am using the querytool DBeaver because I don't have direct access to PostgreSQL. The ultimate goal is to link the PostgreSQL with PowerBi.
select *
from public.address 
where "ID"  in (select max("ID")
from public.address a 
group by "Enterprise_Nbr")

Queries for greatest-n-per-group problems are typically faster if done using Postgres' proprietary distinct on () operator
select distinct on ("Enterprise_Nbr") *
from public.address
order by "Enterprise_Nbr", "ID" desc;

Your query could rewrite as: per each value of Enterprise_Nbr, retrieve row which there is not exists other rows that have same Enterprise_Nbr and greater ID.
SELECT *
FROM public.address a
WHERE NOT EXISTS (
SELECT 1
FROM public.address b
WHERE b.Enterprise_Nbr = a.Enterprise_Nbr AND b.ID > a.ID
)

Related

How to restrict oracle LIKE clause results

I have an Oracle table with 20 million users,
I would like to query the table for users with first name Like "Patel" or "Pat" performance when querying using "like clause" is very bad.
select * from users where first name like '%Patel%'
Or
select * from users where first name like '%Pat%'
And as far as I know if I will restrict the results by rownum - it will happen only after the LIKE - so I have a full table scan...
I don't want to scan the entire 20 Million records
select * from users where first name like '%Pat%' where rownum<100
Is it possible to tell oracle to stop after finding 100 rows?
Oracle 12c (finally) introduced the fetch first syntax, which should perform a bit better:
SELECT *
FROM users
WHERE first_name LIKE '%Pat%'
FETCH FIRST 100 ROWS ONLY
select * from users where first name like '%Pat%' where rownum<100
Oracle is smart enough to do everything for You. Execution plan for this query is:
SELECT STATEMENT, GOAL = ALL_ROWS
COUNT STOPKEY
TABLE ACCESS FULL
COUNT STOPKEY means that full scan will be stopped when Oracle will find enough records to satisfy the condition.
Since the question is tagged Oracle 11g, I'll give an answer that works in 11g.
Use the optimizer hint for first_rows_100 and wrap it into an inline view.
Example:
select *
from (select /*+ opt_param('optimizer_mode','first_rows_100') */
u.*, rownum as rn
from users u
where instr (name, 'Pat') > 0 or instr (name, 'Patel') > 0) inlineview
where rn <= 100
Regards
Olafur

counting rows in select clause with DB2

I would like to query a DB2 table and get all the results of a query in addition to all of the rows returned by the select statement in a separate column.
E.g., if the table contains columns 'id' and 'user_id', assuming 100 rows, the result of the query would appear in this format: (id) | (user_id) | 100.
I do not wish to use a 'group by' clause in the query. (Just in case you are confused about what i am asking) Also, I could not find an example here: http://mysite.verizon.net/Graeme_Birchall/cookbook/DB2V97CK.PDF.
Also, if there is a more efficient way of getting both these results (values + count), I would welcome any ideas. My environment uses zend framework 1.x, which does not have an ODBC adapter for DB2. (See issue http://framework.zend.com/issues/browse/ZF-905.)
If I understand what you are asking for, then the answer should be
select t.*, g.tally
from mytable t,
(select count(*) as tally
from mytable
) as g;
If this is not what you want, then please give an actual example of desired output, supposing there are 3 to 5 records, so that we can see exactly what you want.
You would use window/analytic functions for this:
select t.*, count(*) over() as NumRows
from table t;
This will work for whatever kind of query you have.

implementing paging logic in DB2 SQL

Is there a way to implement paging logic in DB2 SQL, where records can be fetched page wise.
The following query works only for queries with no joins. When queries with join are used the ROW_NUM is returned as 0 and paging cannot be done.
SELECT * FROM (SELECT ROWNUMBER() OVER() AS ROW_NUM, Results.*
FROM (SELECT * FROM Table1 ) AS Results) AS PagedResults
WHERE PagedResults.ROW_NUM>0 AND PagedResults.ROW_NUM<=10
Thanks in advance
How to query range of data in DB2 with highest performance?

Number of rows in Oracle SQL Select?

I need to know how many records were returned in a select in oracle. Currently, I do two queries:
SELECT COUNT(ITEM_ID) FROM MY_ITEMS;
SELECT * FROM MY_ITEMS;
I need to know the COUNT but I hate doing two queries. Is there a way to do:
SELECT * FROM MY_ITEMS
and then find out how many records are in there?
Is there a way to do:
SELECT * FROM MY_ITEMS
and then find out how many records are in there?
If you want it to be in this exact order, you can fetch all records on the client and count their number (almost all client libraries provide a function for that).
You can also do:
SELECT i.*, COUNT(*) OVER ()
FROM my_items i
, which will return you the count along with each record.
If you're working in PL/SQL, you can use the SQL%ROWCOUNT pseudo-variable to get the number of rows affected by the last SQL statement. Might save you some effort.
This ought to do the trick.
WITH
base AS
(
SELECT *
FROM MY_ITEMS
)
SELECT (SELECT COUNT(*) FROM base) kount,
base.*
FROM base
I'm just unsure about the table aliases, I don't remember in Oracle if they require 'AS' or not. But this should work.
select mt.*, c.Cntr
from MyTable mt
, (select COUNT(*) as Cntr
from MyTable
) c

Show only the first N lines of output of a SQL query

Is there a way to only show the first N lines of output from an SQL query? Bonus points, if the query stops running once the N lines are outputted.
I am most interested in finding something which works in Oracle.
It would be helpful if you specify what database you are targetting. Different databases have different syntax and techniques to achieve this:
For example in Oracle you can ahieve this by putting condition on RowNum (select ... from ... where ... rownum < 11 -> would result in outputting first 10 records)
In MySQL you can use you can use limit clause.
Microsoft SQL Server => SELECT TOP 10 column FROM table
PostgreSQL and MySQL => SELECT column FROM table LIMIT 10
Oracle => select * from (SELECT column FROM table ) WHERE ROWNUM <= 10 (thanks to stili)
Sybase => SET rowcount 10 SELECT column FROM table
Firebird => SELECT FIRST 10 column FROM table
NOTE: Modern ORM tools such as Hibernate give high level API (Query, Restriction, Condition interfaces) that abstract the logic of top n rows based on the dialect you choose.
For Oracle the suggested and accepted solution is wrong. Try using an order clause, and the results will be unpredictable. The SQL will need to be nested to accomplish this in Oracle.
select name, price
from (
select name, price, row_number() over (order by price) r
from items
)
where r between 1 and 5;
The example above was borrowed from http://www.adp-gmbh.ch/ora/sql/examples/first_rows.html which has a good discussion on this topic.
I know it with MySQL but I don't know if it's standard SQL :
end you Query with 'limit X', X = n. of lines you want to get.
Example :
SELECT NAME FROM EMPLOYEES ORDER BY SALARY DESC LIMIT 10;
For Oracle, you can try this
select /*+ FIRST_ROWS(10) */ * from table;