Почему делает кардинальность индекса в MySQL, остаются неизменными, когда я добавляю новый индекс?

Возможно, вы захотите посмотреть, как работает закрытие javascript .

Даст базовое представление о том, что происходит.

useEffect будет выполняться только один раз, из-за зависимости. Поэтому, когда рендерились дочерние компоненты items, был пустой массив ([]). У ваших дочерних компонентов всегда будет этот экземпляр items, так как useEffect не вызывается с обновленным items.

Надеюсь, это помогло.

РЕДАКТИРОВАТЬ: 1

Возможно, вы захотите, чтобы ваш родительский компонент выглядел следующим образом.

function Parent(props) {
    const [items, setItems] = useState([]);

    function getItems () {
        console.log(items); // always prints "[]" 
    }

    useEffect(() => {
        thirdPartyApiCall().then((fetchedItems) =>
            setItems(newItems);
        );
    }, []);

    return newItems.map(item => )
}

10
задан 4 revs, 2 users 100% 17 April 2009 в 07:46
поделиться

2 ответа

If you only have 1 row in the table, the cardinality for the index should be 1, of course. It's just counting the number of unique values.

If you think of an index as a lookup-table based on buckets (like a hash), then the cardinality is the number of buckets.

Here's how it works: When you build an index over a set of columns (a,b,c,d), then the database goes over all the rows in the table, looking at the ordered quadruplets of those 4 columns, for each row. Let's say your table looks like this:

a  b  c  d  e   
-- -- -- -- --  
1  1  1  1  200 
1  1  1  1  300
1  2  1  1  200
1  3  1  1  200

So what the database looks at is just the 4 columns (a,b,c,d):

a  b  c  d  
-- -- -- --
1  1  1  1 
1  2  1  1 
1  3  1  1 

See that there are only 3 unique rows left? Those will become our buckets, but we'll get back to that. In reality, there's also a record id, or row identifier for each row in the table. So our original table looks like this:

(row id) a  b  c  d  e   
-------- -- -- -- -- --  
00000001 1  1  1  1  200 
00000002 1  1  1  1  300
00000003 1  2  1  1  200
00000004 1  3  1  1  200

So when we look at only the 4 columns of (a,b,c,d), we're really looking also at the row id:

(row id) a  b  c  d 
-------- -- -- -- --
00000001 1  1  1  1
00000002 1  1  1  1
00000003 1  2  1  1
00000004 1  3  1  1

But we want to do lookup by (a,b,c,d) and not by row id, so we produce something like this:

(a,b,c,d) (row id)
--------- --------
1,1,1,1   00000001
1,1,1,1   00000002
1,2,1,1   00000003
1,3,1,1   00000004

And finally, we group all the row ids of rows that have identicle (a,b,c,d) values together:

(a,b,c,d) (row id)
--------- ---------------------
1,1,1,1   00000001 and 00000002
1,2,1,1   00000003
1,3,1,1   00000004

See that? The values of (a,b,c,d), which are (1,1,1,1) (1,2,1,1) and (1,3,1,1) have become keys for our lookup table into the rows of the original table.

Actually, none of this really happens, but it should give you a good idea on how a "naive" (i.e. straight-forward) implementation of an index might be done.

But the bottom line is this: cardinality just measures how many unique rows there are in an index. And in our example that was the number of keys in our lookup table, which was 3.

Hope that helps!

14
ответ дан 3 December 2019 в 16:10
поделиться

Я не могу однозначно ответить, почему MySQL не вычисляет мощность, но я могу догадаться. В руководстве по MySQL говорится:

Количество элементов: оценка количества уникальных значений в индексе. Это обновляется при запуске ANALYZE TABLE или myisamchk -a. Количество элементов рассчитывается на основе статистики, хранящейся в виде целых чисел, поэтому значение не обязательно является точным даже для небольших таблиц. Чем выше количество элементов, тем больше вероятность, что MySQL использует индекс при выполнении соединений.

Индексы FULLTEXT используются только в запросах MATCH ... AGAINST (...), что вызывает использование индекса. Синтаксис MATCH ... AGAINST не работает, если в этих полях нет индекса FULLTEXT.

Я предполагаю, что количество элементов не вычисляется, потому что это действительно не нужно .

12
ответ дан 3 December 2019 в 16:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: