Postgres query to make faster [closed] - postgresql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Any opinion on which query will be faster ( or no difference) and why?
select * from A a left join B b on a.id = b.cid where b.cl1 = 's1' or b.c1 ='s2' or b.c1 ='s3' ;
or
select * from A a left join B b on a.id = b.cid where b.cl1 in ('s1','s2','s3');
The above is just examples to provide idea on what I am trying to do. May not be the exact syntax.
The reason for asking the question is , I built up a spring-hibernate-JPA query similar to the first one and at this point performance is poor. Hence looking for all possible ways to make the performance better. It may or may not be the query. But not being expert on DB side, looking for information

The second query is always faster, because it can use a single index scan if you have an index on b.cl1.
But since the first query has the conditions on different columns, the queries are quite different, and it makes little sense to compare them.

In such scenarios, you can simply analyze your query by using PostgreSQL query analize and see which one is speed.
Update: As #MatBailie said on his comment, LEFT JOIN requires an ON
clause.
EXPLAIN ANALYSE
SELECT *
FROM A a
LEFT JOIN B ON b
WHERE b.cl1 = 's1'
OR b.c2 = 's2'
OR b.c3 = 's3';
EXPLAIN ANALYSE
SELECT *
FROM A a
LEFT JOIN B ON b
WHERE b.cl1 IN ('s1', 's2', 's3');

Related

SQL - Best practice for handling mass arbitrary data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a massive delimited file and many normalized tables to input the data. Is there a best practice for bringing in the data and inserting the data into its proper fields and tables?
For instance, right now I've created a temp table that holds all the arbitrary data. Some logic runs against each row to determine what values will be going in to what table. Without too much specifics the part that concerns me looks something like:
INSERT INTO table VALUES (
(SELECT TOP 1 field1 FROM #tmpTable),
(SELECT TOP 1 field30 FROM #tmpTable),
(SELECT TOP 1 field2 FROM #tmpTable),
...
(SELECT TOP 1 field4 FROM #tmpTable))
With that, my questions are: Is it reasonable to be using a temp table for this purpose? And is it poor practice to use these SELECT statements so liberally like this? It feels sort of hacky, are there a better ways to handle mass data importing and separation like this?
You should try SSIS.
SSIS How to Create an ETL Package

slick & scala : What are TableQueries? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am a bit disappointed with slick & its TableQueries : the model of an application can be a "class Persons(tag: Tag) extends Table[Person] for example (where Person is a case class with some fields like name, age,address...).
The weird point is the "val persons = TableQuery[Persons]" contains all the records.
To have for example all the adults, we can use:
adults = persons.filter(p => p.age >= 18).list()
Is the content of the database loaded in the variable persons?
Is there on the contrary a mechanism that allows to evaluate not "persons" but "adults"?(a sort of lazy variable)?
Can we say something like 'at any time, "persons" contains the entire database'?
Are there good practices, some important ideas that can help the developer?
thanks.
You are mistaken in your assumption that persons contains all of the records. The Table and TableQuery classes are representations of a SQL table, and the whole point of the library is to ease the interaction with SQL databases by providing a convenient, scala-like syntax.
When you say
val adults = persons.filter{ p => p.age >= 18 }
You've essentially created a SQL query that you can think of as
SELECT * FROM PERSONS WHERE AGE >= 18
Then when you call .list() it executes that query, transforming the result rows from the database back into instances of your Person case class. Most of the methods that have anything to do with slick's Table or Query classes will be focused on generating Queries (i.e. "select" statements). They don't actually load any data until you invoke them (e.g. by calling .list() or .foreach).
As for good practices and important ideas, I'd suggest you read through their documentation, as well as take a look at the scaladocs for any of the classes you are curious about.
http://slick.typesafe.com/docs/

EntityFramework how to: select Max(Column), Count(*) from Table [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get sum of two columns in one LINQ query without grouping
how to select Max and Count in one call using EF?
Select Max([Column]), Count(*) from [Table]
I think you can fake it grouping by 0. SQL Server will see through that constant group-by and eliminate it. It has no runtime cost.
The question is: Will EF translate this to SQL properly? Given the track record (in contrast to good old trusty LINQ to SQL!) this is in doubt.

Optimising (My)SQL Query

I usually use ORM instead of SQL and I am slightly out of touch on the different JOINs...
SELECT `order_invoice`.*
, `client`.*
, `order_product`.*
, SUM(product.cost) as net
FROM `order_invoice`
LEFT JOIN `client`
ON order_invoice.client_id = client.client_id
LEFT JOIN `order_product`
ON order_invoice.invoice_id = order_product.invoice_id
LEFT JOIN `product`
ON order_product.product_id = product.product_id
WHERE (order_invoice.date_created >= '2009-01-01')
AND (order_invoice.date_created <= '2009-02-01')
GROUP BY `order_invoice`.`invoice_id`
The tables/ columns are logically names... it's an shop type application... the query works... it's just very very slow...
I use the Zend Framework and would usually use Zend_Db_Table_Row::find(Parent|Dependent)Row(set)('TableClass') but I have to make lots of joins and I thought it'll improve performance by doing it all in one query instead of hundreds...
Can I improve the above query by using more appropriate JOINs or a different implementation? Many thanks.
The query is wrong, the GROUP BY is wrong. All columns in the SELECT-part that are not in an aggregate function, have to be in the GROUP BY. You mention only one column.
Change the SQL Mode, set it to ONLY_FULL_GROUP_BY.
When this is done and you have a correct query, use EXPLAIN to find out how the query is executed and what indexes are used. Then start optimizing.

What RDBMS command is used to tell what user has what permissions on a particular object? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
What RDBMS command is used to tell what user has what permissions on a particular object?
That depends on the database system you use. In Oracle, you can find out a lot by
select * from all_tab_privs;
Heres how to do it in sql server 2005
select dp.NAME AS principal_name,
dp.type_desc AS principal_type_desc,
o.NAME AS object_name,
p.permission_name,
p.state_desc AS permission_state_desc
sys.database_permissions p
OUTER JOIN sys.all_objects o
on p.major_id = o.OBJECT_ID
inner JOIN sys.database_principals dp
on p.grantee_principal_id = dp.principal_id