Is it more efficient to bind parameters or use excluded in an upsert query? [closed] - postgresql

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 days ago.
Improve this question
I have a table I need to write an upsert statement for. Since I'm using Go the query kinda looks like this:
INSERT
INTO
my_table(
col1,
col2
)
VALUES (
$1,
$2
)ON
CONFLICT(col1) DO
UPDATE
SET
col2 = $2
I'm just wondering if there's any benefit to instead use the following query:
INSERT
INTO
my_table(
col1,
col2
)
VALUES (
$1,
$2
)ON
CONFLICT(col1) DO
UPDATE
SET
col2 = EXCLUDED.col2
Thanks in advance!

Related

How to execute a PostgreSQL with "WITH" statement? [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 1 year ago.
Improve this question
I need to execute a query with WITH statement. If I run it, my WITH data shows error. This is my sample query please guide me to execute it.
with
x1_fields as
(SELECT
x1.id as x_id,
x1.created_time as x_created,
sum(x1.value) as x_sum
FROM
xxxx1 x1
INNER JOIN xxxx2 x2 ON x2.id = x1.id
INNER JOIN xxxx3 x3 ON x3.id = x2.id
WHERE
x1.customer = 'microsoft'
GROUP BY
x1.id,
x1.created_time
)
SELECT
y.id as final_id,
x_t.sum as final_sum
FROM
yyyy as y
left join x1_fields as x_t on x_t.x_id = y.id
where
y.customer = 'microsoft'
and y.id = '123456'
I have try to run this query in DBeaver but I can't run this. But it works in SSRS. I need to check such queries in some SQL software and debug it. Please give me some instructions to solve this.
Since we don't know your database and table structure, we cannot find an error which might occure because of wrong relation names or something.
However: Syntax error is the , character after the WITH clause (before the final SELECT). You have to remove it.

How to increase the speed sql query and get fastly record [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 2 years ago.
Improve this question
I have a large set of records in my database. I tried to run SQL query using where condition. I got the data and I show count of records also. It taking around 21 secs. I need to reduce the time.
Sql Query
select * from sample_rank where company_id = 1
Records Count
166270266
I added Index in db. but It's not fastly getting data. How to fix it and How to change the query
Assuming, going by your comments, that you only need to select two of the columns in your table:
SELECT col1, col2 FROM sample_rank WHERE company_id = 1;
then the following covering index is probably the best you can do here:
CREATE INDEX idx ON sample_rank (company_id, col1, col2);
The above index completely covers the entire query, meaning that, if used, your SQL engine can use the index alone to satisfy the entire query plan. I put "if" in bold, because depending on the cardinality of the data, the above index might not help the query run faster. For example, if you only have two company_id values, 1 and 2, with 50% of the record having each value, then your SQL engine might just decide that a full table scan would be faster than even resorting to use the index. As suggested in the comments, running EXPLAIN on your actual query would reveal more information.

Combine the distinct column of same table in db2 [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 3 years ago.
Improve this question
I want to combine distinct column of an one table, I want a column wise distinct.
SELECT ALT_SRC_HOST FROM ATABLE WHERE ALT_SRC_HOST IS NOT null
UNION
SELECT SOURCE_IP AS SOURCE_IP FROM ATABLE WHERE SOURCE_IP IS NOT null
output is coming, in one column. I want the output is in two column
expected output
ALT_SRC_HOST SOURCE_IP
- 10.262.737.21
1.34.34.112
Not sure why you want it in a single SQL but this is an option
select distinct ALT_SRC_HOST as col1 , NULL as col2
from ATABLE
union all
select distinct NULL as col1 , SOURCE_IP as col2
from ATABLE

what is getting wrong with query ? [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
INSERT INTO EMP_COMPANY(ename,cname,salary,jdate) VALUES
('ANIL','ACC',1500.00,'01-MAY-89'),
........
('AMOL','ACC',1000.00,'17-MAR-95');
Error: ORA-00933: SQL command not properly ended .
what is missing in syntax ?
You cannot multiple records like the way you are doing. You can try like this:
INSERT INTO EMP_COMPANY(ename,cname,salary,jdate)
select 'ANIL','ACC',1500.00,'01-MAY-89' from dual
union all
select 'SHANKAR','TATA',2000.00,'10-MAY-90' from dual
union all
select 'JAYA','CMC',1800.00,'7-JULY-91' from dual
union all
select 'SUNIL','CMC',1700.00,'1-JAN-88' from dual
union all
select 'VIJAY','TATA',5000.00,'3-JAN-88' from dual
union all
select 'PRAKASH','TATA',3000.00,'27-MAY-89' from dual
There is one more option to use INSERT ALL like this:
INSERT ALL
INTO EMP_COMPANY (ename,cname,salary,jdate) VALUES ('ANIL','ACC',1500.00,'01-MAY-89')
INTO EMP_COMPANY (ename,cname,salary,jdate) VALUES ('SHANKAR','TATA',2000.00,'10-MAY-90')
INTO EMP_COMPANY (ename,cname,salary,jdate) VALUES ('JAYA','CMC',1800.00,'7-JULY-91')
...........
SELECT 1 FROM DUAL;

print the 1 to 10 numbers using Table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to print the 1 to 10 numbers based on table.
Table1
A
null
outout is
1
2
3
4
.
.
10
I have a table that table contains only one record...using this table i want to print 1 to 10 numbers please tell me
I written like this...is there any alternate way to write this
select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 from table1;
or
select generate_series(1,10) from table1;
Because your question is almost devoid of useful information, I don't feel bad simply presenting a link to the docs. Take a look at generate_series()
http://www.postgresql.org/docs/current/static/functions-srf.html