I'm creating 4 tables and select from them afterwards. Selecting works perfectly for the first 3 select statements, but the 4th one takes about 10 seconds on the iPhone simulator, and 5 seconds on the sqlite3 console.
Also I get 0 results on the iPhone simulator, but 1 on the console. But that's a problem I want to solve after I solved the performance issue.
I read something about indexes and how they can improve the performance, but I have no clue how to implement them in my code.
sql0 = [[NSString alloc]initWithFormat:#"
create table v%i
as select id_produkt
from v%i natural join produkt_eigenschaft
where id_eigenschaft =
(select id_eigenschaft from eigenschaft where at = '%#')",counter,counter-1,selectedStringItem];
and afterwards:
NSString *sqleig = [[NSString alloc]initWithFormat:#"
select at
from eigenschaft
where id_eigenschaft IN
(select distinct id_eigenschaft
from produkt_eigenschaft
where id_produkt IN (select * from v%i))
AND rubrik = '%i'",counter-1, [sender tag] + 1];
Why is this statement executed so slowly? And how can I solve it?
Thanks in advance.
EDIT: explain query plan and .schema
explain query plan create table v3 as select id_produkt from v2 natural join produkt_eigenschaft where id_eigenschaft = (select id_eigenschaft from eigenschaft where at = '101-170');
0|0|1|SCAN TABLE produkt_eigenschaft (~100000 rows)
0|0|0|EXECUTE SCALAR SUBQUERY 1
1|0|0|SEARCH TABLE eigenschaft USING AUTOMATIC COVERING INDEX (at=?) (~7 rows)
0|1|0|SEARCH TABLE v2 USING AUTOMATIC COVERING INDEX (id_produkt=?) (~7 rows)
explain query plan select at from eigenschaft where id_eigenschaft IN (select distinct id_eigenschaft from produkt_eigenschaft where id_produkt IN (select * from v3)) AND rubrik = '5';
0|0|0|SCAN TABLE eigenschaft (~10000 rows)
0|0|0|EXECUTE LIST SUBQUERY 1
1|0|0|SCAN TABLE produkt_eigenschaft (~100000 rows)
1|0|0|EXECUTE LIST SUBQUERY 2
2|0|0|SCAN TABLE v3 (~1000000 rows)
1|0|0|USE TEMP B-TREE FOR DISTINCT
CREATE TABLE eigenschaft (id_eigenschaft integer,rubrik integer,en text,at text,ba text,bg text,hr text,cz text,hu text,pl text,ro text,ru text,rs text,sk text,si text);
CREATE TABLE farbe (id_farbe integer,hexcode text,farbton integer,farbname text);
CREATE TABLE produkt (id_produkt integer,code text,pdf_link text,image_link text,image_small blob,link text,en text,at text,ba text,bg text,hr text,cz text,hu text,pl text,ro text,ru text,rs text,sk text,si text,active integer);
CREATE TABLE produkt_eigenschaft (id_produkt integer,id_eigenschaft integer);
CREATE TABLE produkt_farbe (id_produkt integer,id_farbe integer);
CREATE TABLE produkt_surface (id_surface integer,id_produkt integer,image_link text);
CREATE TABLE produkt_text (id_produkt integer,en text,at text,ba text,bg text,hr text,cz text,hu text,pl text,ro text,ru text,rs text,sk text,si text);
CREATE TABLE rubrik (id integer,en text,at text,ba text,bg text,hr text,cz text,hu text,pl text,ro text,ru text,rs text,sk text,si text);
CREATE TABLE v0(id_produkt INT);
CREATE TABLE v1(id_produkt INT);
CREATE TABLE v2(id_produkt INT);
CREATE TABLE v3(id_produkt INT);
Solved it with indexes.
create index i on produkt_eigenschaft (id_eigenschaft)
Call this once before selecting
Related
I'm having trouble finding the correct syntax.
I have
SELECT
A.[Journal ISSN], A.[ISSN Primary], A.[ISSN Secondary], A.[Journal Title],
pubYear = B.[Key],
ImpactFactor = B.[Value]
INTO
A_ADMIN_IMPACT_FACTORS_2020_ROWS
FROM
A_ADMIN_IMPACT_FACTORS_2020 A
CROSS APPLY
(SELECT *
FROM OPENJSON((SELECT A.* For JSON Path, Without_Array_Wrapper))
WHERE [Key] NOT IN ('ID', 'ISSN Primary', 'ISSN Secondary', 'URL_Data', 'Journal ISSN', 'Journal Title', 'Other', 'Columns', 'ToExclude', 'Category')) B
And this works well when I drop the Table and and run the code. What I would like to do is to change it into an INSERT statement so it would start like
INSERT INTO A_ADMIN_IMPACT_FACTORS_2020_ROWS
([Journal ISSN], [ISSN Primary], [ISSN Secondary], [Journal Title], .....
IF need be I can create a temp table convert the columns into rows and THEN add them but if there is a syntax that would work and skip that step that would work better.
Syntax INSERT SELECT:
INSERT INTO A_ADMIN_IMPACT_FACTORS_2020_ROWS
([Journal ISSN],[ISSN Primary],[ISSN Secondary],[Journal Title], ...)
Select A.[Journal ISSN],A.[ISSN Primary], A.[ISSN Secondary], A.[Journal Title]
,pubYear = B.[Key] ,ImpactFactor = B.[Value]
From A_ADMIN_IMPACT_FACTORS_2020 A
Cross Apply ( Select *
From OpenJson((Select A.* For JSON Path,Without_Array_Wrapper ))
Where [Key] not in ('ID','ISSN Primary','ISSN Secondary','URL_Data','Journal ISSN'
,'Journal Title','Other','Columns','ToExclude', 'Category')
) B
I am working on sql server I have two tables and I need to return records from the left table which are not found in the right table for that I am using left join like below query,
select #MID=MID,#MName=Name,#PID=PID,#PName=PName,#DID=DID from #CompanyDataInfo where id=#MCount
insert into #temp SELECT Top(1) f.Name,f.PID,f.PName,v.* FROM #CompanyDataInfo f
left join Employee v on v.Id=f.ID and v.DID=f.DID
where v.Id =#MID and v.DId = #DId and v.PId = #PId and v.CId =#CId and DATE_TIME between DATEADD(minute,-555,GETDATE()) and GETDATE() order by DATE_TIME desc
Result should be all rows from #CompanyDataInfo table while no record found in Employee table for related ID, I googled and use "v.Id is null" but not getting expected result
Is there any solution greatly appriciable
Thanks In advance
Your query is not using left join in correct way. You are using your right table reference in where clause. I try to correct it below but I don't have full information about your table schema. Please try this-
select
#MID = MID,
#MName = Name,
#PID = PID,
#PName = PName,
#DID = DID
from #CompanyDataInfo
where id = #MCount
insert into #temp
select
f.Name,
f.PID,
f.PName,
v.*
from #CompanyDataInfo f
left join Employee v on v.Id=f.ID and v.DID=f.DID
where f.Id = #MID and
f.DId = #DId and
f.PId = #PId and
f.CId = #CId and
f.DATE_TIME between DATEADD(minute,-555,GETDATE()) and GETDATE() and
v.Id is null
order by f.DATE_TIME desc
Add ...and v.Id is null to your where clause.
Any way to improve the performance of this store procedure?
For example combining both updates clauses in a unique statement (T-SQL).
BonosConvenio and CON_Convenio are almost 5K rows.
create table #resultados (
monto_deuda float
,Cuenta float
,Rut varchar(10)
,CodCONVENIO varchar(4)
,CONVENIO varchar(50)
,Bono varchar(15)
,MontoBono float
,ApellDeudor varchar(100)
)
INSERT INTO #resultados(Cuenta,monto_deuda,Rut,CodCONVENIO,CONVENIO,Bono,MontoBono,ApellDeudor)
select ATC_CTA_Correlativo, monto_deuda, RutDeudor, CodCONVENIO,'','',0,''
from CartaCobProgramasCONVENIO
where Nomina = #Nomina
UPDATE #resultados
SET Bono=bc.Bono,
MontoBono=bc.Monto
FROM BonosCONVENIO bc
WHERE bc.ATC_CTA_Correlativo=#resultados.Cuenta;
UPDATE #resultados
SET ApellDeudor=upper(rtrim(ltrim(Paciente.PAC_PAC_Nombre))
FROM BD_ENTI_CORPORATIVA..ATC_Cuenta Cuenta, BD_ENTI_CORPORATIVA..PAC_Paciente Paciente
WHERE Cuenta.ATC_CTA_Correlativo=#resultados.Cuenta AND Paciente.PAC_PAC_Numero = Cuenta.PAC_PAC_Numero
UPDATE #resultados
SET CONVENIO=c.CON_CON_Descripcio
FROM BD_ENTI_CORPORATIVA..CON_Convenio c
WHERE c.CON_CON_Codigo =#resultados.CodCONVENIO
SELECT * FROM #resultados ORDER BY Cuenta,Bono
End
Try this:
SELECT
ccpc.ATC_CTA_Correlativo Cuenta
, ccpc.monto_deuda
, ccpc.RutDeudor Rut
, ccpc.CodCONVENIO
, c.CON_CON_Descripcio CONVENIO
, bc.Bono
, bc.Monto MontoBono
, UPPER(RTRIM(LTRIM(Paciente.PAC_PAC_Nombre))) ApellDeudor
FROM
CartaCobProgramasCONVENIO ccpc
INNER JOIN
BonosCONVENIO bc ON
ccpc.ATC_CTA_Correlativo = bc.ATC_CTA_Correlativo
INNER JOIN
BD_ENTI_CORPORATIVA..ATC_Cuenta Cuenta ON
ccpc.ATC_CTA_Correlativo = Cuenta.ATC_CTA_Correlativo
INNER JOIN
BD_ENTI_CORPORATIVA..PAC_Paciente Paciente ON
Cuenta.PAC_PAC_Numero = Paciente.PAC_PAC_Numero
INNER JOIN
BD_ENTI_CORPORATIVA..CON_Convenio c ON
c.CON_CON_Codigo = ccpc.CodCONVENIO
WHERE Nomina = #Nomina;
If you have duplicate records because of the joins, it's because there are multiple values in the tables. Your original query (using an update) would overwrite and only show the last row's value. To work around this, you would probably want to have a GROUP BY and MIN, MAX, SUM or whatever.
I have two tables table1 and table2. Each table contains a column with itemPrice. I need to add the two columns together.
The SQL query below returns the correct SUM.
SELECT SUM(item1+ item2) FROM
(select SUM(t1.itemPrice) item1 from table1 t1 WHERE t1.userid=='jonh') tableA
CROSS JOIN
(select SUM(t2.itemPrice) item2 from table2 t2 WHERE t1.userid=='jonh') tableB
I am not been lazy but the above query has so many SUM functions that I don't know where to start to write LINQ queries.
Can anyone help?
Ceci,
Hopefully this will give you what you want...
from f in (
from x in ( from t1 in Table1
where t1.Userid.Equals("John")
select new { Userid = t1.Userid }
).Distinct()
select new { item1 = ( from z in Table1
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 ,
item2 = ( from z in Table2
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 }
) select new { total = f.item1 + f.item2 }
In the case where there are no records for "john" in one table, it will bring back a 0 and sum up the other tables.
hope this helps.
I have multiple tables in a join and every table has a column ID. So in the resultig join there are a lot of ID columns. How I can access a specific ID column with the criteria API?
ParameterExpression<A> idParam = criteriaBuilder.parameter(A.class, PARAM_NAME);
Subquery<B> sq = query.subquery(B.class);
Root<B> root = sq.from(B.class);
Join<C, D> joinTogether = root.join("memberX").join("memberY");
sq.select(root);
sq.where(criteriaBuilder.and(criteriaBuilder.equal(joinTogether.get("id"), idParam), criteriaBuilder.equal(parentQuery.get("id"), root.get("id"))));
The problem is, that in the resulting SQL contains
SELECT 1 FROM E t6, B t5, C t4, D t3 WHERE ((( = paramName) AND (t0.ID = t5.ID)) AND (((t6.memberZ = t5.ID) AND (t4.ID = t6.memberX)) AND (t3.ID = t4.memberY))))
The table E (t6) is an additional join table between table B and C, t0 is the reference to the parent query. Instead t3.id = :paramName EclipseLink creates nothing just before the first equal-sign (paramName is the content of the constant PARAM_NAME). My idea is, that the "id" column could reference all tables and EclipseLink can not decide, which table I mean.
How I can change that?
Thank you
André