How to select rows from partition in MySQL
Asked Answered
L

8

20

I made partition my 300MB table and trying to make select query from p0 partition with this command

SELECT * FROM employees PARTITION (p0);

But I am getting following error

ERROR 1064 (42000): You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near '(p0)' at line 1

How to write select query to get data from specific partition?

Lichtenfeld answered 1/1, 2013 at 16:53 Comment(3)
Check this post: dba.stackexchange.com/questions/23138/…. It's not supported.Hadwyn
Why do you need that construct? Can't you simply add a WHERE clause that hits p0?Ferry
Can you share how you created the table+partition?Erg
V
14

Depending on you MySql version, PARTITION keyword does not exist until MySQL 5.6.2. You would be using MySQL 5.5 or even 5.1, but not 5.6. In case, you are using MySQL 5.1, then you can do some workaround like below

SELECT partition, count(ID)
FROM
(
    SELECT ID,
      case when condition then p1
           when condition then p2
      .....
      end as partition

    FROM
      table
) s1
GROUP BY partition

Note : The above solution is just workaround to get you desire output.

You may also try this query to count total number of rows for your partition.

SELECT table_rows as 'count(*)' FROM information_schema.partitions WHERE table_schema = schema() and table_name ='employees' and partition_name = 'p0';

Note : you may change table_schema = schema() to table_schema = 'yourschema'

Violoncellist answered 1/1, 2013 at 17:2 Comment(15)
ok no issue, just don't forget to accept, other will get the benefitVioloncellist
Sorry, i tested it and it seems this command selects all rowsLichtenfeld
then, what was your problem?? you just said, you were unable to select rows. that is it.Violoncellist
I want to select rows from p0 partition of table. My table has 138167 row and result is : 138167 rows in set (17.42 sec)Lichtenfeld
As i mentioned above, check the quoted post. this SELECT is not supported in mysql 5.1Jurgen
Yes i saw, if i cant select from partition why making partition allowed with this version?Lichtenfeld
please check my updated post, that may help you to figure out, why it is there.Violoncellist
find out the difference, when you executed the first query and when you executed the second oneVioloncellist
please check my Note, you need to change that with your schema name, whatever would be your schema name, just change it with thatVioloncellist
I used second command that you wrote and returned this : +----------+ | count(*) | +----------+ | 16612 | +----------+ 1 row in set (0.00 sec)Lichtenfeld
have you notice anything ?? now you got exact row count from your own partition (less number than previous one).Violoncellist
Yes i already know that partition done before. But my need is select rows from that partition. I think this row count comes from information_schema.partitions not actual partition.Lichtenfeld
Hey, select * from employees p0; - this is completely wrong! It returns all results, since p0 is treated as the table alias and not the partition name.Finnie
@Lichtenfeld any specific reason for removing from accepted answer after 4 Years ? :DVioloncellist
@ravi I was just checking my questions and accountLichtenfeld
L
22

Actually since MySQL 5.6 the supported syntax is:

SELECT * FROM table PARTITION (partitionName);
Lunate answered 6/6, 2015 at 2:52 Comment(1)
Clear and precise! :)Nimble
V
14

Depending on you MySql version, PARTITION keyword does not exist until MySQL 5.6.2. You would be using MySQL 5.5 or even 5.1, but not 5.6. In case, you are using MySQL 5.1, then you can do some workaround like below

SELECT partition, count(ID)
FROM
(
    SELECT ID,
      case when condition then p1
           when condition then p2
      .....
      end as partition

    FROM
      table
) s1
GROUP BY partition

Note : The above solution is just workaround to get you desire output.

You may also try this query to count total number of rows for your partition.

SELECT table_rows as 'count(*)' FROM information_schema.partitions WHERE table_schema = schema() and table_name ='employees' and partition_name = 'p0';

Note : you may change table_schema = schema() to table_schema = 'yourschema'

Violoncellist answered 1/1, 2013 at 17:2 Comment(15)
ok no issue, just don't forget to accept, other will get the benefitVioloncellist
Sorry, i tested it and it seems this command selects all rowsLichtenfeld
then, what was your problem?? you just said, you were unable to select rows. that is it.Violoncellist
I want to select rows from p0 partition of table. My table has 138167 row and result is : 138167 rows in set (17.42 sec)Lichtenfeld
As i mentioned above, check the quoted post. this SELECT is not supported in mysql 5.1Jurgen
Yes i saw, if i cant select from partition why making partition allowed with this version?Lichtenfeld
please check my updated post, that may help you to figure out, why it is there.Violoncellist
find out the difference, when you executed the first query and when you executed the second oneVioloncellist
please check my Note, you need to change that with your schema name, whatever would be your schema name, just change it with thatVioloncellist
I used second command that you wrote and returned this : +----------+ | count(*) | +----------+ | 16612 | +----------+ 1 row in set (0.00 sec)Lichtenfeld
have you notice anything ?? now you got exact row count from your own partition (less number than previous one).Violoncellist
Yes i already know that partition done before. But my need is select rows from that partition. I think this row count comes from information_schema.partitions not actual partition.Lichtenfeld
Hey, select * from employees p0; - this is completely wrong! It returns all results, since p0 is treated as the table alias and not the partition name.Finnie
@Lichtenfeld any specific reason for removing from accepted answer after 4 Years ? :DVioloncellist
@ravi I was just checking my questions and accountLichtenfeld
T
4

The correct form of the query is as below it works for me fine.

select * from employees partition (`p0`);
Transfigure answered 6/9, 2018 at 10:52 Comment(0)
J
3

You are right, explicit selection of PARTITION is not supported in 5.1.54 Version. See this post

Jurgen answered 1/1, 2013 at 17:2 Comment(0)
F
3

i was pulling my hair out and then realised my mistake

select * from user_info as ui PARTITION (`p0`) 

That query fails because the alias has to be specified after the partition list

 select * from user_info PARTITION (`p0`) as ui
Fusil answered 22/2, 2022 at 20:25 Comment(0)
H
1

It's not supported in current version of MYSQL.

Check this question on DBA. You may also check out MYSQL dev article

Hadwyn answered 1/1, 2013 at 17:9 Comment(1)
Please add all relevant information to the question instead of linking to external pagesScreening
B
1

SELECT * FROM invoice_detail PARTITION (p1);

Bowyer answered 8/2, 2020 at 6:39 Comment(3)
Please do not provide querys that are widely open for SQL injectionScreening
@NicoHaase You should explain what you mean a little more. How is that susceptible to injection?Geoffrey
@Geoffrey just have a look at the previous version of that answerScreening
D
-1

I think its worth pointing out to others that may stumble upon this page, that a 300MB table does not need a partition.

300MB is a trivial amount of data for any modern (or even not so modern) database and thus everything will work better if you leave your data in one table in one database in one partition.

Decile answered 8/2, 2020 at 8:20 Comment(1)
It doesn't answer the OP question plus its not specified the potential growth of this table during time to recommend not partitioning or whatsoever. You might share your opinions as a comments not answers to questions.Federica

© 2022 - 2024 — McMap. All rights reserved.