MySQL의 테이블 필드에 인덱스가 존재하는지 어떻게 확인합니까?

StackOverflow https://stackoverflow.com/questions/127156

  •  02-07-2019
  •  | 
  •  

문제

이것을 몇 번 구글해야했기 때문에 Q/A를 공유하고 있습니다.

도움이 되었습니까?

해결책

사용 SHOW INDEX 그렇게 :

SHOW INDEX FROM [tablename]

문서 : https://dev.mysql.com/doc/refman/en/show-index.html

다른 팁

노력하다:

SELECT * FROM information_schema.statistics 
  WHERE table_schema = [DATABASE NAME] 
    AND table_name = [TABLE NAME] AND column_name = [COLUMN NAME]

인덱스에 주어진 이름을 알 필요가없는 특정 열에 어떤 종류의 색인이 있는지 알려줍니다. 또한 저장된 절차에서도 작동합니다 (색인 표시와는 반대로)

SHOW KEYS FROM  tablename WHERE Key_name='unique key name'

테이블에 고유 한 키가 있는지 확인할 수 있습니다.

show index from table_name where Column_name='column_name';

다음 진술을 사용하십시오. 색인 표시 your_table

그런 다음 필드의 결과를 확인합니다 : row [ "table"], row [ "key_name"

"key_name"을 올바르게 작성하십시오

CLI의 테이블 레이아웃 만 살펴보십시오. 당신은 할 것입니다

desc mytable

또는

표시 테이블 mytable

열에 대한 색인이 존재하는 경우 (여기서는 순서대로) 데이터베이스 기능으로 기능이 필요한 경우이 코드를 사용/채택 할 수 있습니다. 멀티 컬럼 인덱스의 위치에 관계없이 색인이 전혀 존재하는지 확인하려면 "및 seq_in_index = 1"을 삭제하십시오.

DELIMITER $$
CREATE FUNCTION `fct_check_if_index_for_column_exists_at_first_place`(
    `IN_SCHEMA` VARCHAR(255),
    `IN_TABLE` VARCHAR(255),
    `IN_COLUMN` VARCHAR(255)
)
RETURNS tinyint(4)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'Check if index exists at first place in sequence for a given column in a given table in a given schema. Returns -1 if schema does not exist. Returns -2 if table does not exist. Returns -3 if column does not exist. If index exists in first place it returns 1, otherwise 0.'
BEGIN

-- Check if index exists at first place in sequence for a given column in a given table in a given schema. 
-- Returns -1 if schema does not exist. 
-- Returns -2 if table does not exist. 
-- Returns -3 if column does not exist. 
-- If the index exists in first place it returns 1, otherwise 0.
-- Example call: SELECT fct_check_if_index_for_column_exists_at_first_place('schema_name', 'table_name', 'index_name');

-- check if schema exists
SELECT 
    COUNT(*) INTO @COUNT_EXISTS
FROM 
    INFORMATION_SCHEMA.SCHEMATA
WHERE 
    SCHEMA_NAME = IN_SCHEMA
;

IF @COUNT_EXISTS = 0 THEN
    RETURN -1;
END IF;


-- check if table exists
SELECT 
    COUNT(*) INTO @COUNT_EXISTS
FROM 
    INFORMATION_SCHEMA.TABLES
WHERE 
    TABLE_SCHEMA = IN_SCHEMA
AND TABLE_NAME = IN_TABLE
;

IF @COUNT_EXISTS = 0 THEN
    RETURN -2;
END IF;


-- check if column exists
SELECT 
    COUNT(*) INTO @COUNT_EXISTS
FROM 
    INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_SCHEMA = IN_SCHEMA
AND TABLE_NAME = IN_TABLE
AND COLUMN_NAME = IN_COLUMN
;

IF @COUNT_EXISTS = 0 THEN
    RETURN -3;
END IF;

-- check if index exists at first place in sequence
SELECT 
    COUNT(*) INTO @COUNT_EXISTS
FROM 
    information_schema.statistics 
WHERE 
    TABLE_SCHEMA = IN_SCHEMA
AND TABLE_NAME = IN_TABLE AND COLUMN_NAME = IN_COLUMN
AND SEQ_IN_INDEX = 1;


IF @COUNT_EXISTS > 0 THEN
    RETURN 1;
ELSE
    RETURN 0;
END IF;


END$$
DELIMITER ;

다음 SQL 문을 사용하여 테이블의 주어진 열을 확인할 수 있습니다.

select  a.table_schema, a.table_name, a.column_name, index_name
from    information_schema.columns a
join    information_schema.tables  b on a.table_schema  = b.table_schema and
                                    a.table_name = b.table_name and 
                                    b.table_type = 'BASE TABLE'
left join (
 select     concat(x.name, '/', y.name) full_path_schema, y.name index_name
 FROM   information_schema.INNODB_SYS_TABLES  as x
 JOIN   information_schema.INNODB_SYS_INDEXES as y on x.TABLE_ID = y.TABLE_ID
 WHERE  x.name = 'your_schema'
 and    y.name = 'your_column') d on concat(a.table_schema, '/', a.table_name, '/', a.column_name) = d.full_path_schema
where   a.table_schema = 'your_schema'
and     a.column_name  = 'your_column'
order by a.table_schema, a.table_name;

조인은 innodb_sys_*에 대한 것이므로 일치 색인은 Innodb 테이블에서만 제공됩니다.

인덱스가 존재하지 않으면 오류가 발생하므로 특정 표시 인덱스 쿼리를 실행할 수 없습니다. 따라서 SQL 오류를 피하려면 모든 인덱스를 배열로 가져 와서 반복해야합니다.

그녀는 내가 어떻게하는지. 테이블에서 모든 인덱스를 가져옵니다 (이 경우, leads) 그런 다음 Foreach 루프에서 열 이름 (이 경우, province) 존재 여부.

$this->name = 'province';

$stm = $this->db->prepare('show index from `leads`');
$stm->execute();
$res = $stm->fetchAll();
$index_exists = false;

foreach ($res as $r) {
    if ($r['Column_name'] == $this->name) {
        $index_exists = true;
    }
}

이렇게하면 인덱스 속성을 실제로 좁힐 수 있습니다. do a print_r$res 작업 할 수있는 것을보기 위해.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top