Correlation via primary key column - mysql

I have two tables with unequal rows of data that I need to correlate with via "3rd-party" column. This 3rd-party column is a DATETIME primary key for both tables.
Here are the tables
|XKey |XColumn |
|-------------------------------|
|2012-01-00 |0.17
|2012-02-00 |0.24
|2012-03-00 |0.33
|2012-04-00 |0.41
|--------------------------------
Y TABLE
=======
|YKey |YColumn |
|-------------------------------|
|2012-01-01 |0.21
|2012-01-15 |0.12
|2012-02-01 |0.26
|2012-02-15 |0.27
|2012-03-01 |0.14
|2012-03-15 |0.11
|2012-04-01 |0.74
|2012-04-15 |0.66
|2012-05-01 |0.14
|--------------------------------
The values in y table should correlate to corresponding values in the x table whose xkey value is less than or equal to the respective ykey values.
This prepared statement is simply yielding null! What could I be overlooking?
SET #sql_run = CONCAT(
'SET #correl = (SELECT (1/(?-1))*SUM(((a.'
,XColumn
,' - ?)/?)*((b.'
,YColumn
,' - ?)/?)) FROM '
,XTable
,' a JOIN '
,YTable
,' b ON a.'
,XKey
,' <= b.'
,YKey
,' GROUP BY a.'
,XKey
,', b.'
,YKey
,' HAVING COUNT(a.'
,XKey
,') < 2 ORDER BY a.'
,XKey
,', b.'
,YKey
,')'
);
PREPARE statement_run FROM #sql_run ;
EXECUTE statement_run USING #num, #x_avg, #x_std, #y_avg, #y_std ;
DEALLOCATE PREPARE statement_run ;
Having adjuusted the date values in xtable am now getting the error 'subquery returns more than one row"

Your XKey date values are invalid. Try setting them to the first of the month instead of 0.

Related

How to improve slow query performance?

I have a multi-join query that targeting the hospital's chart database.
this takes 5~10 seconds or more.
This is the visual expain using mysql workbench.
The query is below.
select sc.CLIENT_ID as 'guardianId', sp.PET_ID as 'patientId', sp.NAME as 'petName'
, (select BW from syn_vital where HOSPITAL_ID = sp.HOSPITAL_ID and PET_ID = sp.PET_ID order by DATE, TIME desc limit 1) as 'weight'
, sp.BIRTH as 'birth', sp.RFID as 'regNo', sp.BREED as 'vName'
, (case when ss.NAME like '%fel%' or ss.NAME like '%cat%' or ss.NAME like '%pawpaw%' or ss.NAME like '%f' then '002'
when ss.NAME like '%canine%' or ss.NAME like '%dog%' or ss.NAME like '%can%' then '001' else '007' end) as 'sCode'
, (case when LOWER(replace(sp.SEX, ' ', '')) like 'male%' then 'M'
when LOWER(replace(sp.SEX, ' ', '')) like 'female%' or LOWER(replace(sp.SEX, ' ', '')) like 'fam%' or LOWER(replace(sp.SEX, ' ', '')) like 'woman%' then 'F'
when LOWER(replace(sp.SEX, ' ', '')) like 'c.m%' or LOWER(replace(sp.SEX, ' ', '')) like 'castratedmale' or LOWER(replace(sp.SEX, ' ', '')) like 'neutered%' or LOWER(replace(sp.SEX, ' ', '')) like 'neutrality%man%' or LOWER(replace(sp.SEX, ' ', '')) like 'M.N%' then 'MN'
when LOWER(replace(sp.SEX, ' ', '')) like 'woman%' or LOWER(replace(sp.SEX, ' ', '')) like 'f.s%' or LOWER(replace(sp.SEX, ' ', '')) like 'S%' or LOWER(replace(sp.SEX, ' ', '')) like 'neutrality%%' then 'FS' else 'NONE' end) as 'sex'
from syn_client sc
left join syn_tel st on sc.HOSPITAL_ID = st.HOSPITAL_ID and sc.CLIENT_ID = st.CLIENT_ID
inner join syn_pet sp on sc.HOSPITAL_ID = sp.HOSPITAL_ID and sc.FAMILY_ID = sp.FAMILY_ID and sp.STATE = 0
inner join syn_species ss on sp.HOSPITAL_ID = ss.HOSPITAL_ID and sp.SPECIES_ID = ss.SPECIES_ID
WHERE
trim(replace(st.NUMBER, '-','')) = '01099999999'
and trim(sc.NAME) = 'johndoe'
and sp.HOSPITAL_ID = 'HOSPITALID999999'
order by TEL_DEFAULT desc
I would like to know how to improve the performance of this complex query.
The most obvious performance killers in your query are the non-sargable criteria in your where clause.
trim(replace(st.NUMBER, '-','')) = '01099999999'
This cannot use any available index as you have applied a function to the column, which needs to be evaluated before the comparison can be made.
As suggested by Pham, you could change your criterion to -
st.number IN ('01099999999', '01-099-999-999', 'ALL_OTHERS_FORMAT_YOU_ACCEPTS...')
or better still would be to normalize the numbers before you store them (you can always apply formatting for display purposes), that way you know how to search the stored data. Strip all the hyphens and spaces from the existings numbers -
UPDATE syn_tel
SET number = REPLACE(REPLACE(number, '-',''), ' ', '')
WHERE number LIKE '% %' OR number LIKE '%-%';
Similarly for the next criterion -
trim(sc.NAME) = 'johndoe'
The name should be trimmed before being stored in the database so there is no need to trim it when searching it. Update already stored names to trim whitespace -
UPDATE syn_client
SET NAME = TRIM(NAME)
WHERE NAME LIKE ' %' OR NAME LIKE '% ';
Changing sp.HOSPITAL_ID = 'HOSPITALID999999' to sc.HOSPITAL_ID = 'HOSPITALID999999' will allow for the use of a composite index on syn_client (HOSPITAL_ID, name) assuming you drop the TRIM() from the previously discussed criterion.
The sorting in your sub-query for weight might be wrong -
order by DATE, TIME desc limit 1
presumably you want the most recent weight -
order by `DATE` desc, `TIME` desc limit 1
/* OR */
order by CONCAT(`DATE`, ' ', `TIME`) desc limit 1
order by DATE, TIME desc -- really? That's equivalent to date ASC, time DESC. If you want "newest first", then ORDER BY date DESC, time DESC. Furthermore, it is usually bad practice and clumsy to code when you have DATE and TIME in separate columns. Is there a strong reason for storing them separately? It is reasonably easy to split them apart in a SELECT.
Similarly, cleanse NUMBER and NAME when inserting.
This will make the first subquery much faster:
syn_vital needs INDEX(hostital_id, pet_id, date, time, BW)
LIKE with a leading wildcard (%) is slow, but you probably cannot avoid it in this case.
LOWER(replace(sp.SEX, ' ', '')) -- Cleanse the input during INSERT, not on output!.
LOWER(...) -- With a suitable COLLATION (eg, the default), calling LOWER is unnecessary.
Some of these 'composite' INDEXes may be useful:
ss: INDEX(HOSPITAL_ID, SPECIES_ID, NAME)
st: INDEX(HOSPITAL_ID, CLIENT_ID, NUMBER)
sp: INDEX(HOSPITAL_ID, PET_ID)
What table is TEL_DEFAULT in?
You may want to:
Create index on syn_client(hospital_id, name --,tel_default?)
Create index on syn_tel(hospital_id, client_id, number)
Create index on syn_pet(hospital_id, family_id, state)
Create index on syn_species(hospital_id, species_id)
Change your query to:
SELECT ...
FROM syn_client sc
INNER JOIN syn_tel st ON sc.hospital_id = st.hospital_id AND sc.client_id = st.client_id
INNER JOIN syn_pet sp ON sc.hospital_id = sp.hospital_id AND sc.family_id = sp.family_id AND sp.state = 0
INNER JOIN syn_species ss ON sp.hospital_id = ss.hospital_id AND sp.species_id = ss.species_id
WHERE st.number IN ('01099999999', '01-099-999-999', 'ALL_OTHERS_FORMAT_YOU_ACCEPTS...')
AND trim(sc.name) = 'johndoe' --sc.name = 'johndoe' with standardize data input
AND sc.hospital_id = 'HOSPITALID999999' --not sp.hospital_id
ORDER BY tel_default DESC;

Auto increment temporary column in select statement MySQL 8

Hello i use this type of sentence to return an auto increment column in my prepared statement selects
cnt := cnt + 1
SET #query = CONCAT('SELECT * FROM (SELECT (#cnt := #cnt + 1) AS id, a.idProducto idProducto, a.idExProducto idExterno, trim(a.descripcion) nombreProducto, trim(a.descripcionAlt) nombreLargoProducto, trim(a.serial) serial,',
' a.peso, a.volumen, a.IdTpoGrupo, trim(b.descripcion) nombreGrupo,',
' a.idTpoLinea, trim(c.descripcion) nombreLinea,',
' a.idTpoMarca, trim(d.descripcion) nombreMarca,',
' a.idTpoUnidad, trim(e.descripcion) nombreTipoUnidad,',
' CASE WHEN a._estado = 1 THEN ''true'' ELSE ''false'' END estado, ',
' g.nombreArchivo imagen ',
' FROM tblProducto a',
' INNER JOIN tblProducto_TpoGrupo b ON a.IdTpoGrupo = b.IdTpoGrupo',
' INNER JOIN tblProducto_TpoLinea c ON a.idTpoLinea = c.idTpoLinea',
' INNER JOIN tblProducto_TpoMarca d ON a.idTpoMarca = d.idTpoMarca',
' INNER JOIN tblTpoUnidadMedida e ON a.idTpoUnidad = e.idTpoUnidadMed ',
' LEFT JOIN tblProductoXImagen f ON a.idProducto = f.idProducto AND f._estado=1',
' LEFT JOIN tblImagen g on f.idImagen = g.idImagen AND g._estado=1',
' WHERE a._estado<2 ',whereLike,whereConcat, ' order by idProducto ) allrecords');
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
that works perfect on MySQL 5, but now in MySQL 8 is deprecated and my transaction return a warning, but the warning stop the execution of my stored procedure, i want to know if there is a solution to can achieve this behavior in MySQL 8.
Thanks for your help.
You can use window functions. The logic you sem to want is:
row_number() over(order by idProducto) as id
This gives you an incrementing integer value, that starts at 1 and increments according to idProducto. If two rows in the resultset have the same idProducto, it is undefined which one will be ordered "first" (it is, however, guaranteed that they will not get the same row number) - in that case, you might want to add one or more columns to the order by clause, so you do get a predictable, stable result.

return the Nth word from database

I want to get nth word from a column I'm using a code line and it works for me but there is an issue, for example:
First line is: "the Nth word from database"
Second line is: "return the Nth word from database and more words"
When I search for 6th word 'database' it returns my first line and second line but I don't want to get my first line because it has only 5 words.
thank you all
My code line:
SELECT *,
SUBSTRING_INDEX(SUBSTRING_INDEX(`Text`, ' ', 6), ' ', -1) as Nth
FROM `tbl_name`
Having six words in you sentence means that you have to have at least five spaces, adding simlpe condition will resolve your problem:
select *,
case when length(`text`) - length(replace(`text`, ' ', '')) >= 5 then
substring_index(replace(`text`, substring_index(`text`, ' ', 5) , ''), ' ', 2)
else null end Nth
from `tbl_name`
Also I changed your query, because it didn't take into account that you might not have 6th space (exactly six words).
Demo
Or even more concicse:
select *,
substring_index(substring_index(`text`, ' ', 5 - (length(`text`) - length(replace(sentence`text` ' ', ''))) - 1), ' ', 1)
from `tbl_name`
Another demo.
You should update your query with where clause, in where you can count the number of words by the following query.
SELECT *, SUBSTRING_INDEX(SUBSTRING_INDEX(`Text`, ' ', 6), ' ', -1) as Nth
FROM `tbl_name`
where (COUNT(column1) - LENGTH(replace(column1, ' ', '')) > 5
You should have to take count of spaces or whichever string which you want to take & then need to apply having-clause on that count.
SELECT
* ,
SUBSTRING_INDEX( SUBSTRING_INDEX( `text` , ' ', 6 ) , ' ', -1 ) AS Nth,
ROUND( ( LENGTH( `text` ) - LENGTH( REPLACE( `text` , " ", "" ) ) ) / LENGTH( " " ) ) AS countq
FROM `xp_test`
HAVING
countq >= 5

Replacing an string on a field via MySQL

I have the following field (which is a string) of one of my tables:
'[{"id":"6","value":["http://www.google.com","http://www.google.com","new"]},
{"id":"7","value":"Reuters"},
{"id":"20","value":"2017/03/17"}]'`
As you can see, the last part, the one with id=20 has a date on it, 2017/03/17.
Can someone tell me how can I update that field so I can get the output 2017-03-17?
My desired output after the update would be:
'[{"id":"6","value":["http://www.google.com","http://www.google.com","new"]},
{"id":"7","value":"Reuters"},
{"id":"20","value":"2017-03-17"}]'`
If I use:
UPDATE mytable
SET myfield = replace(myfield , '/', '-');
It removes the / of the links, and I want to mantain them.
PS: The ids of the fields are always the same (the one for the date is always 20 for example) the only thing that changes are the values.
you can do it with a query like this:
UPDATE mytable
SET myfield = CONCAT (
SUBSTRING_INDEX(myfield, '"id":"20","value":', 1)
, '"id":"20","value":',
, REPLACE (SUBSTRING_INDEX(myfield, '"id":"20","value":', -1),'/','-')
)
WHERE ....:
sample
mysql> SELECT
-> CONCAT (
-> SUBSTRING_INDEX('[{"id":"6","value":["http://www.google.com","http://www.google.com","new"]},
'> {"id":"7","value":"Reuters"},
'> {"id":"20","value":"2017/03/17"}]', '"id":"20","value":', 1),
-> '"id":"20","value":',
-> REPLACE (SUBSTRING_INDEX('[{"id":"6","value":["http://www.google.com","http://www.google.com","new"]},
'> {"id":"7","value":"Reuters"},
'> {"id":"20","value":"2017/03/17"}]', '"id":"20","value":', -1),'/','-')
-> ) AS result;
+------------------------------------------------------------------------------------------------------------------------------------------------+
| result |
+------------------------------------------------------------------------------------------------------------------------------------------------+
| [{"id":"6","value":["http://www.google.com","http://www.google.com","new"]},
{"id":"7","value":"Reuters"},
{"id":"20","value":"2017-03-17"}] |
+------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)
mysql>
Maybe there is a better way, But you can :
split the string into rows using { as separator
find the row with "ID=20"
do the replace
join all rows back together using GROUP_CONCAT
DEMO
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(T.json, '{', numbers.n), '{', -1) name,
CASE WHEN INSTR(SUBSTRING_INDEX(SUBSTRING_INDEX(T.json, '{', numbers.n), '{', -1), '"id":"20"') > 0
THEN REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(T.json, '{', numbers.n), '{', -1), '/', '-')
ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(T.json, '{', numbers.n), '{', -1)
END as result
FROM
(select 1 n union all
select 2 union all select 3 union all
select 4 union all select 5) numbers
JOIN (
SELECT '[{"id":"6","value":["http://www.google.com","http://www.google.com","new"]},
{"id":"7","value":"Reuters"},
{"id":"20","value":"2017/03/17"}]' as json
) T
on CHAR_LENGTH(T.json)
-CHAR_LENGTH(REPLACE(T.json, '{', ''))>=numbers.n-1
order by
n
OUTPUT
OPTION 2
SELECT SUBSTRING(json, 1, pos_begin - 1 ) as side_left,
REPLACE(SUBSTRING(json, pos_begin, pos_end - pos_begin + 1), '/', '-') as side_middle,
SUBSTRING(json, pos_end + 1, LENGTH(json)) as side_end
FROM (
SELECT LOCATE('{"id":"20"', json) as pos_begin,
LOCATE('}', json, LOCATE('{"id":"20"', json)) as pos_end,
T.json
FROM (
SELECT '[{"id":"6","value":["http://www.google.com","http://www.google.com","new"]},
{"id":"7","value":"Reuters"},
{"id":"20","value":"2017/03/17"}]' as json
) T
) S
;
OUTPUT

MySQL Variable Substitution

I want to simplify the following using dynamic SQL like one could do in Transact SQL.
I want to do something like:
SET #s = replace(field_name, '_complete','')
and use #s instead of replace(field_name, '_complete','')
Please adive if possible and if so how.
My current code:
select distinct
if(instr(replace(field_name, '_complete',''),'_') <= 5
,left(replace(field_name, '_complete','')
,instr(replace(field_name, '_complete',''),'_') - 1
)
,replace(field_name, '_complete','')
) AS form_id ,replace(
if(instr(replace(field_name, '_complete',''),'_') <= 5,
mid(replace(field_name, '_complete',''),
instr(replace(field_name, '_complete',''),'_') + 1,
length(replace(field_name, '_complete','')) - instr(replace(field_name, '_complete',''),'_')
)
,replace(field_name, '_complete','')
),
'_',
' ') as form_name ,field_name from redcap_extract2use where field_name like '%_complete' order by 1;
The above would then be replaced with:
select distinct
if(instr(#s,'_') <= 5 ,left(#s,instr(#s,'_') - 1),#s ) AS form_id
,replace( if(instr(#s,'_') <= 5,
mid(#s,instr(#s,'_') + 1,length(#s) - instr(#s,'_')),#s), '_', ' ') as form_name
,field_name
from redcap_extract2use
where field_name like '%_complete'
order by 1;
and I would have an execute... to run the query
If I'm understanding your question correctly then you would want to use the PREPARE and the EXECUTE statements.
For example:
SET #s = replace(field_name, '_complete','');
PREPARE mystatement FROM
SELECT DISTINCT ...... ;
EXECUTE mystatement;