The ideal amount of sleep for middle to old age is seven hours each night.
The study found that both too much and too little sleep was linked to worse mental health. It appeared that a consistent amount of sleep was beneficial.
You got worse if you moved away from seven hours. The processes that go on in our brain during sleep are very important for maintaining our physical and mental health.
She said that getting a good night's sleep was important at all stages of life.
Slow-wave, or deep, sleep, has been shown to be important for memory consolidation and may be a reason for the link between insufficient sleep and cognitive decline. A lack of deep sleep could prevent the brain from clearing toxins.
The data from the UK Biobank was used for the study. It found that the brain area with the most affected by sleep was the hippocampus, the brain's memory centre, with too much or too little sleep linked to a smaller brain volume. The analysis found that people who slept seven hours a night performed better on cognitive tests for processing speed, visual attention, memory and problem-solving skills.
The link between sleep and some brain disorders is complex and the work could not prove a causality. Unusual sleep patterns and insomnia are common symptoms of dementia.
The scientists don't know why spending eight or more hours in bed could cause problems. People who have poor quality, disturbed sleep tend to spend longer sleeping or trying to sleep because they feel tired.
While we can't say that too little or too much sleep causes cognitive problems, our analysis looking at individuals over a longer period of time appears to support this idea.
The reasons why older people have poorer sleep appear to be complex, influenced by a combination of our genetic makeup and the structure of our brains.