سؤال

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?

هل كانت مفيدة؟

المحلول 2

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'

نصائح أخرى

Actually since MySQL 5.6 the supported syntax is:

SELECT * FROM table PARTITION (partitionName);

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

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

select * from employees partition (`p0`);

It's not supported in current version of MYSQL.

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

SELECT * FROM invoice_detail PARTITION (p1);

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top