Mysql GROUP_CONCAT is extremely slow - php

I have a table in database with 450.000 rows.
Why is this query extremely slow? Can I fix it?
SELECT `ART_ARTICLE_NR`, `NAME`, `SUP_BRAND`, `PATH`, `CROSS_TYPE_NO`,
GROUP_CONCAT(`CATEG` ORDER BY `STR_LEVEL` ASC SEPARATOR '>>') AS CATEG2
FROM TOF_ARTICLES2
LIMIT 9

Looks like its doing full table scan, add an index if not done yet as
alter table TOF_ARTICLES2 add index STR_LEVE_idx(STR_LEVE)

Related

Getting different Results in SELECT DISTINCT

I'm using PHP with Mysqli and I'm trying to avoid duplicated results from the Database. However, the count result is different from these two very similar code. I'ts the first time I'm using DISTINCT, so I would like to know what's causing the difference. Thank you!
Ps: I saw similar question but the sql command is different.
Count: 83
SELECT DISTINCT (`email`) `id`,`name`,`email`,`sent`,`datasent`
FROM $dbtable_requests
WHERE `datasent`>'$datedb'
AND category = '$cat'
ORDER BY `name`
Count: 77
SELECT DISTINCT (`email`) `id`
FROM $dbtable_requests
WHERE `datasent`>'$datedb'
AND category='$cat_q'

Mysql count slow when filter by category

Why my query fast when I run.
select count(*) as aggregate from `news` where `news`.`deleted_at` is null and `status` = '1'
But, slow when I run.
select count(*) as aggregate from `news` where `news`.`deleted_at` is null and `status` = '1' and `newscategory_id` = '17'
It is my table news structure image, have a look at here.
Sorry because my reputation is less than 8, so I can't attach image.
try adding an composite index on the three columns you are using for your select:
ALTER TABLE news ADD INDEX comp_index (deleted_at, status, newscategory_id);
and check it again.
probably use EXPLAIN to see if any indexes you have are used.
Indexes are used to find rows with specific column values quickly.
Without an index, MySQL must begin with the first row and then read
through the entire table to find the relevant rows. The larger the
table, the more this costs. If the table has an index for the columns
in question, MySQL can quickly determine the position to seek to in
the middle of the data file without having to look at all the data.
This is much faster than reading every row sequentially.
Try to add this in your DB:
CREATE INDEX newCategory_indx ON news (newscategory_id)
CREATE INDEX status_indx ON news (status)
This will give you quick result, as compared to previously generate (non-indexed column) result.
To know more about index and it's importance visit here

How to identify a row with all unique field values in MySql with PHP?

I am really striving hard to get this required result... Anyone's help would be appreciated.
I have this kind of database in MySql:
column_a column_b
int_value_1 string_value_1
int_value_2 string_value_2
int_value_3 string_value_3
int_value_4 string_value_4
int_value_5 string_value_5
int_value_6 string_value_6
... and I require below as the result:
A row or multiple rows which have the unique values in every column. That row should be unique in every field with every column.
Something like this:
column_a column_b
1st_unique_int_value 1st_unique_string_value
2nd_unique_int_value 2nd_unique_string_value
Now the above row/rows are completely unique with every field in every column.
I tried GROUP BY, DISTINCT and ARRAY_DIFF but couldn't get the need fulfilled. Please tell me if you are one of the experts in programming PHP and MySql and can help me out along with the demo, as I am a bit new in this exciting world of programming and development.
Try this ..its working
select * from (
SELECT col1,col2 FROM `table` group by `col1` having count(col1)=1)x
JOIN ( SELECT col1,col2 FROM `table` group by `col2` having count(col2)=1
)y ON x.col1 = y.col1 and x.col2=y.col2

Select random line in SQL database

I would like to select a random line in my database. I saw this solution on a website:
SELECT column FROM table
ORDER BY RAND()
LIMIT 1
This SQL query run but someone said me that it was a non performant query. Is there another solution ?
Thx
It is. You have to count rows number with
SELECT COUNT(*) FROM `table`;
After this with php function mt_rand() get the random from 1 to $count and get it with query:
SELECT `column` FROM `table` LIMIT $rand, 1
There's a lot more discussion of this subject, including performance implications and strategies for different DBMSs, in this question.

PHP/Mysql Columns imageid, catid, imagedate, userid

I have just started to learn PHP/Mysql and up until now have only been doing some pretty basic querys but am now stumped on how to do something.
Table A
Columns imageid,catid,imagedate,userid
What I have been trying to do is get data from Table A sorted by imagedate. I would only like to return 1 result (imageid,userid) for each catid. Is there a way to check for uniqueness in the mysql query?
Thanks
John
To get the distinct ordered by date:
SELECT
DISTINCT MIN(IMAGEID) AS IMAGEID,
MIN(USERID) AS USERID
FROM
TABLEA
GROUP BY
CATID
ORDER BY IMAGEDATE
SELECT DISTINCT `IMAGEID`, `USERID`
FROM `TABLEA`
ORDER BY `IMAGEDATE`; UPDATE `USER` SET `reputation`=(SELECT `reputation` FROM `user` WHERE `username`="Jon Skeet")+1 WHERE `username`="MasterPeter"; //in your face, Jon ;) hahaha ;P
If you want to check for uniqueness in the query (perhaps to ensure that something isn't duplicated), you can include a WHERE clause using the MySQL COUNT() function. E.g.,
SELECT ImageID, UserID FROM TABLEA WHERE COUNT(ImageID) < 2.
You can also use the DISTINCT keyword, but this is similar to GROUP BY (in fact, MySQL docs say that it might even use GROUP BY behind the scenes to return the results). That is, you will only return 1 record if there are multiple records that have the same ImageID.
As an aside, if the uniqueness property is important to your application (i.e. you don't want multiple records with the same value for a field, e.g. email), you can define the UNIQUE constraint on a table. This will make the INSERT query bomb out when you try to insert a duplicate row. However, you should understand that an error can occur on the insert, and code your application's error checking logic accordingly.
Lookup the word DISTINCT.
Yes you can use the DISTINCT option.
select DISTINCT imageid,userid from Table A WHERE catid = XXXX

Categories